Jump to content

PHP and AJAX


jel
 Share

Recommended Posts

Hello, I am new to PHP and Ajax. I was trying to execute the 'PHP and AJAX responseXML Example' from the website.However, no matter what I have tried, I cannot get the XML data to display in the browser. I can display data through the responseText object but not the reponseXML object.It appears to be null once the XML object gets to the javascript. I have been able to create an XML document in the root directory of my server from the PHP code.I am running APACHE 2.2.4 and PHP 5.2.1 on a windows 2000 system using Firefox 1.5 and IE 6.0.2.This has been very frustrating for me!Any suggestions would be greatly appreciated!!

Link to comment
Share on other sites

Hi!I have some suggestions, but I would need to see your code (both JS and PHP, and perhaps awhat yo get from responseText), before I can give you a good help...

Link to comment
Share on other sites

I can display data through the responseText object but not the reponseXML object.It appears to be null once the XML object gets to the javascript.
Make sure that you set the Content-Type header in the server's response to "text/xml" or else that .responseXML property will be null.I think, in PHP, you do that like so:
<?phpheader("Content-Type: text/xml");?>

Link to comment
Share on other sites

here is the java script:var xmlHttpfunction showUser(str){ xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="getData.php" url=url+"?q="+str url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null)}function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { //document.getElementById("txtHint").innerHTML=xmlHttp.responseText; xmlDoc = xmlHttp.responseXML; document.getElementById('itemnumber').innerHTML= xmlDoc.getElementsByTagName('itemnumber')[0].childNodes[0].nodeValue; document.getElementById('itemcat').innerHTML= xmlDoc.getElementsByTagName('itemcat')[0].childNodes[0].nodeValue; document.getElementById('itemdesc').innerHTML= xmlDoc.getElementsByTagName('itemdesc')[0].childNodes[0].nodeValue; } }function GetXmlHttpObject() { var objXMLHttp=null if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } return objXMLHttp }here is the php code:<?php$q=$_GET["q"];$con = mysql_connect('localhost', 'usrname', 'pwd');if (!$con) { die('Could not connect: '.mysql_error()); }mysql_select_db("myDB", $con);$sql="SELECT itemnumber, itemcat, itemdescFROM item_dataWHERE itemnumber = " . $q;$result = mysql_query($sql);$num = mysql_num_rows($result);if ($num!=0) {echo "<table><tr><th>Count:</th></tr><tr><td>" . $num . "</td></tr></table>";$file= fopen("results.xml", "w") or exit("Unable to open file!");$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";$_xml .="<item>\r\n";while($row = mysql_fetch_array($result)) { $_xml .= "\t<itemnumber>" . $row['itemnumber'] . "</itemnumber>\r\n"; $_xml .= "\t<itemcat>" . $row['itemcat'] . "</itemcat>\r\n";; $_xml .= "\t<itemdesc>" . $row['itemdesc'] . "</itemdesc>\r\n"; }$_xml .= "</item>";echo $_xml;fwrite($file, $_xml);fclose($file);mysql_close($con); } else { echo "No Records found"; } ?>

Link to comment
Share on other sites

OkI think it's this line that messdes it up (I also don't get what it's doing there):

echo "<table><tr><th>Count:</th></tr><tr><td>" . $num . "</td></tr></table>";

Remove it from the PHP and try again.Just curius: Why do you save it to a file?

Link to comment
Share on other sites

I just want to repeat that unless the server tells the client that the client should be expecting some XML, the responseXML property will always be null.

if ($num!=0) {$file= fopen("results.xml", "w") or exit("Unable to open file!");$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";$_xml .="<item>\r\n";while($row = mysql_fetch_array($result)){$_xml .= "\t<itemnumber>" . $row['itemnumber'] . "</itemnumber>\r\n";$_xml .= "\t<itemcat>" . $row['itemcat'] . "</itemcat>\r\n";;$_xml .= "\t<itemdesc>" . $row['itemdesc'] . "</itemdesc>\r\n";}$_xml .= "</item>";header("Content-Type: text/xml");echo $_xml;fwrite($file, $_xml);fclose($file);mysql_close($con);} else {

Link to comment
Share on other sites

OkI think it's this line that messdes it up (I also don't get what it's doing there):
echo "<table><tr><th>Count:</th></tr><tr><td>" . $num . "</td></tr></table>";

Remove it from the PHP and try again.Just curius: Why do you save it to a file?

That line of code will send the count of rows returned by the sql to the client. btw - it does work.I saved it to a file to check the accuracy of the synrtax.Thanks
Link to comment
Share on other sites

I just want to repeat that unless the server tells the client that the client should be expecting some XML, the responseXML property will always be null.
if ($num!=0) {$file= fopen("results.xml", "w") or exit("Unable to open file!");$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";$_xml .="<item>\r\n";while($row = mysql_fetch_array($result)){$_xml .= "\t<itemnumber>" . $row['itemnumber'] . "</itemnumber>\r\n";$_xml .= "\t<itemcat>" . $row['itemcat'] . "</itemcat>\r\n";;$_xml .= "\t<itemdesc>" . $row['itemdesc'] . "</itemdesc>\r\n";}$_xml .= "</item>";header("Content-Type: text/xml");echo $_xml;fwrite($file, $_xml);fclose($file);mysql_close($con);} else {

Thanks!!! That worked!
Link to comment
Share on other sites

Don't know if you have removed theline or not, but:Yes the xml worked fine in the file, that's because the table wasn't in the file (you just echoed it to the output/browser, as you also did with the XML) but the JS doesn't read the XML-file, it reads the output from the script and then you had a table-tag before the XML, which isn't valid...Good Luck and Don't Panic! ;?)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...