javascriptsqlajaxxml

This functions runs when the page loads, but only alerts 1 and 2 occur-- not alert 3. What bug is preventing alert 3 from executing?


When the page loads, this function is run, but only gets to alert 2, I suspect it doesn't reach alert 3 due to a bug in the previous line var id_array = xmlDoc.getElementsByTagName("id");. I have tried researching getElementsByTagName() to see if I am using it correctly and I still feel like I am.

var xmlhttp;
xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200){

        var xmlDoc = xmlhttp.responseXML;
    alert("2");
        var id_array = xmlDoc.getElementsByTagName("id");
    alert("3");
    for(id in id_array)
    {
        // do something with the id 
    }
    }
}
xmlhttp.open("POST", "xmlDoc.xml", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(); 
alert("1");

this is a sample of the xml document it should read from. The actual file generates x amount of "id"s with varying information from an sql database.

<id>
    <name><![CDATA[<?php echo $query_array["name"]; ?>]]></name>
    <latitude><![CDATA[<?php echo $query_array["latitude"]; ?>]]></latitude>
    <longitude><![CDATA[<?php echo $query_array["longitude"]; ?>]]></longitude>
    <color><![CDATA[<?php echo $color_array["color_id"]; ?>]]></color>
    <color_r><![CDATA[<?php echo $color_array["red"]; ?>]]></color_r>
    <color_g><![CDATA[<?php echo $color_array["green"]; ?>]]></color_g>
    <color_b><![CDATA[<?php echo $color_array["blue"]; ?>]]></color_b>
    <text><![CDATA[<?php echo $user_array["comment"]; ?>]]></text>
</id>
<id>
    <name><![CDATA[<?php echo $query_array["name"]; ?>]]></name>
    <latitude><![CDATA[<?php echo $query_array["latitude"]; ?>]]></latitude>
    <longitude><![CDATA[<?php echo $query_array["longitude"]; ?>]]></longitude>
    <color><![CDATA[<?php echo $color_array["color_id"]; ?>]]></color>
    <color_r><![CDATA[<?php echo $color_array["red"]; ?>]]></color_r>
    <color_g><![CDATA[<?php echo $color_array["green"]; ?>]]></color_g>
    <color_b><![CDATA[<?php echo $color_array["blue"]; ?>]]></color_b>
    <text><![CDATA[<?php echo $user_array["comment"]; ?>]]></text>
</id>
<id>
    <name><![CDATA[<?php echo $query_array["name"]; ?>]]></name>
    <latitude><![CDATA[<?php echo $query_array["latitude"]; ?>]]></latitude>
    <longitude><![CDATA[<?php echo $query_array["longitude"]; ?>]]></longitude>
    <color><![CDATA[<?php echo $color_array["color_id"]; ?>]]></color>
    <color_r><![CDATA[<?php echo $color_array["red"]; ?>]]></color_r>
    <color_g><![CDATA[<?php echo $color_array["green"]; ?>]]></color_g>
    <color_b><![CDATA[<?php echo $color_array["blue"]; ?>]]></color_b>
    <text><![CDATA[<?php echo $user_array["comment"]; ?>]]></text>
</id>

So my EXPECTED behavior would be that id_array (in this example) gets 3 cells (one per occurence) and each of those cells contains all the data within the tags.

But again, I suspect a bug in the assignment of id_array is keeping anything from happening.

Any help would be great!


Solution

  • set

    xmlhttp.setRequestHeader('Content-Type',  'text/xml');
    

    and see if

    xmlhttp.responseXML
    

    does not yield null resulting in an error when trying to do .getElementsByTagName("id").