sirup_segar Posted February 19, 2008 Share Posted February 19, 2008 hi, after i read php ajax responseXML from w3schools.com, i'd like to try it myself. so i copy-paste the code and have it run from my computer. i use xampp and php5. function showUser(str) { var xmlHttp; xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="responsexml.php" url=url+"?q="+str url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { xmlDoc=xmlHttp.responseXML; document.getElementById("firstname").innerHTML= xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue; document.getElementById("lastname").innerHTML= xmlDoc.getElementsByTagName("lastname")[0].childNodes[0].nodeValue; document.getElementById("job").innerHTML= xmlDoc.getElementsByTagName("job")[0].childNodes[0].nodeValue; document.getElementById("age_text").innerHTML="Age: "; document.getElementById("age").innerHTML= xmlDoc.getElementsByTagName("age")[0].childNodes[0].nodeValue; document.getElementById("hometown_text").innerHTML="<br/>From: "; document.getElementById("hometown").innerHTML= xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue; } } xmlHttp.open("GET",url,true) xmlHttp.send(null) } function GetXmlHttpObject() { var objXMLHttp=null if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } return objXMLHttp } and i got error message on my firefox, xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue has no properties i'm not familiar with xml, so first i'm confuse in where to post this thread, in ajax room,or else. anyway, this is the php code <?php header('Content-Type: text/xml'); header("Cache-Control: no-cache, must-revalidate"); //A date in the past header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); $q=$_GET["q"]; $con = mysql_connect('localhost', 'peter', 'abc123'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ajax_demo", $con); $sql="SELECT * FROM user WHERE id = ".$q.""; $result = mysql_query($sql); echo '<?xml version="1.0" encoding="ISO-8859-1"?> <person>'; while($row = mysql_fetch_array($result)) { echo "<firstname>" . $row['FirstName'] . "</firstname>"; echo "<lastname>" . $row['LastName'] . "</lastname>"; echo "<age>" . $row['Age'] . "</age>"; echo "<hometown>" . $row['Hometown'] . "</hometown>"; echo "<job>" . $row['Job'] . "</job>"; } echo "</person>"; mysql_close($con); ?> thanks in advance.. Quote Link to comment Share on other sites More sharing options...
priti Posted February 19, 2008 Share Posted February 19, 2008 it is a javascript error.... first do one thing create a single string for all xml i.e $xmlstr= '<?xml version="1.0" encoding="ISO-8859-1"?> <person>'; while($row = mysql_fetch_array($result)) { $xmlstr.= "<firstname>" . $row['FirstName'] . "</firstname>"; $xmlstr.= "<lastname>" . $row['LastName'] . "</lastname>"; $xmlstr.= "<age>" . $row['Age'] . "</age>"; $xmlstr.= "<hometown>" . $row['Hometown'] . "</hometown>"; $xmlstr.="<job>" . $row['Job'] . "</job>"; } $xmlstr.="</person>"; //do echoing after completion of your xml echo $xmlstr; now after chaging this tey to alert(xmldoc) i.e your responseText and see wether correct xml is received or not.Use firebug(extension in firefox) to track your error. Regards Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.