I have followed this tutorial sequence up to "Show Hint" http://www.w3schools.com/php/php_ajax_php.asp
which runs on my server. But when I uploaded the files for "ShowCD" http://www.w3schools.com/php/php_ajax_xml.asp
it doesn't work.
The html file:
<html><head><script>function showCD(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","getcd.php?q="+str,true);xmlhttp.send();}</script></head><body>
<form>Select a CD:<select name="cds" onChange="showCD(this.value)"><option value="">Select a CD:</option><option value="Bob Dylan">Bob Dylan</option><option value="Bonnie Tyler">Bonnie Tyler</option><option value="Dolly Parton">Dolly Parton</option></select></form><div id="txtHint"><b>CD info will be listed here...</b></div>
</body></html>
Here's the php file:
<?php$q=$_GET["q"];
$xmlDoc = new DOMDocument();$xmlDoc->load("cd_catalog.xml");
$x=$xmlDoc->getElementsByTagName('ARTIST');
for ($i=0; $i<=$x->length-1; $i++){//Process only element nodesif ($x->item($i)->nodeType==1) { if ($x->item($i)->childNodes->item(0)->nodeValue == $q) { $y=($x->item($i)->parentNode); } }}
$cd=($y->childNodes);
for ($i=0;$i<$cd->length;$i++){//Process only element nodesif ($cd->item($i)->nodeType==1) { echo("<b>" . $cd->item($i)->nodeName . ":</b> "); echo($cd->item($i)->childNodes->item(0)->nodeValue); echo("<br>"); }}?>
and here's a snip of the xml file:
<?xml version="1.0" encoding="utf-8" ?>- <!-- Edited by XMLSpy -->- <CATALOG>- <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD>- <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD>
</CATALOG>