XML : XML wont load content through ajax

I'm trying to load an xml file using ajax. I tried to modify the example of W3Schools

  <html>      <head>          <script>              function showBus(str) {                  if (str == "") {                      document.getElementById("txtHint").innerHTML = "";                      return;                  }                  if (window.XMLHttpRequest) {                      // code for IE7+, Firefox, Chrome, Opera, Safari                      xmlhttp = new XMLHttpRequest();                  } else { // code for IE6, IE5                      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");                  }                  xmlhttp.onreadystatechange = function() {                      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {                          document.getElementById("txtHint").innerHTML = xmlhttp.responseText;                      }                  }                    xmlhttp.open("GET", "getbus.php?q=" + str, true);                  xmlhttp.send();              }          </script>      </head>        <body>          <form>Select your bus route:              <select name="NUMBER" onchange="showBus(this.value)">                  <option value="">Select a Bus:</option>                  <option value="102">102</option>              </select>              <div id="txtHint"><b>Bus info will be listed here...</b>              </div>          </form>            
  <?php      $q = $_GET["q"];      $xmlDoc = new DOMDocument();      $xmlDoc->load("routes.xml");      $x = $xmlDoc->getElementsByTagName('NUMBER');      for ($i = 0; $i <= $x->length - 1; $i++) {          //Process only element nodes          if ($x->item($i)->nodeType == 1) {              if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {                  $y = ($x->item($i)->parentNode);              }          }      }        $BUS = ($y->childNodes);        for ($i = 0; $i < $BUS->length; $i++) {          //Process only element nodes          if ($BUS->item($i)->nodeType == 1) {              echo("<b>" . $BUS->item($i)->nodeName . ":</b> ");              echo($BUS->item($i)->childNodes->item(0)->nodeValue);              echo("<br>");          }      }  ?>    

XML File:

  <TT>      <BUS>          <NUMBER>102</NUMBER>          <DEPARTING_STOP>102</DEPARTING_STOP>          <DESTINATION>102</DESTINATION>          <TIME>102</TIME>      </BUS>  </TT>    

This is what I get when I select the bus number from drop down menu:

  Select your bus route:  load("routes.xml"); $x=$xmlDoc->getElementsByTagName('NUMBER'); for ($i=0; $i<=$x->length-1; $i++) { //Process only element nodes if ($x->item($i)->nodeType==1) { if ($x->item($i)->childNodes->item(0)->nodeValue == $q) { $y=($x->item($i)->parentNode); } } } $BUS=($y->childNodes); for ($i=0;$i<$BUS->length;$i++) { //Process only element nodes if ($BUS->item($i)->nodeType==1) { echo("" . $BUS->item($i)->nodeName . ": "); echo($BUS->item($i)->childNodes->item(0)->nodeValue); echo("  "); } } ?>     

No comments:

Post a Comment