Davidammit Posted August 13, 2008 Share Posted August 13, 2008 <unimportant part> I'm trying to make a site that embeds videos. All I want to have to update on said site is a database that will store the embed code. On my quest, I came across several languages that I believed would be of use to me. Javascript, PHP, XML and AJAX. <important part> I saw the example at http://www.w3schools.com/php/php_ajax_responsexml.asp where i thought that would be an excellent way to make it work. But when I tried to modify this code into something I thought would work for my site, it didn't work. I couldn't find out why. So I took the original code and put a bunch of alerts in the javascript portion, to find out that the document.getElementById("hometown").innerHTML= xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue; part wasn't being executed. So I fiddled around with that for a little while. My main test was to change it to document.getElementById("hometown").innerHTML="IT WORKED!!"; and since that worked, I conluded that it must not have been able to find the XML tags or maybe the response wasn't generated. If anyone can explain this or help me fix it I would greatly appreciate it. PS: All I did to get the files was copy and paste the code into Notepad and save as .html, .js, and .php to satisfy the program, so I might need some sort of PHP or JavaScript compiling programs? However I did make the table they use under the user in the database jax_test in MySQL. And I created a user by the name peter with a password of abc123. Quote Link to comment Share on other sites More sharing options...
haku Posted August 13, 2008 Share Posted August 13, 2008 xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue; getElementsByTagName requires tag names - 'div', 'p', 'ul', 'li' etc. You have given it an ID (or so it appears, unless by some chance you have created your own tags called 'hometown'). Quote Link to comment Share on other sites More sharing options...
Davidammit Posted August 13, 2008 Author Share Posted August 13, 2008 In the example the PHP code is supposed to return XML. The HTML: <html> <head> <script src="responsexml.js"></script> </head> <body> <form> Select a User: <select name="users" onchange="showUser(this.value)"> <option value="1">Peter Griffin</option> <option value="2">Lois Griffin</option> <option value="3">Glenn Quagmire</option> <option value="4">Joseph Swanson</option> </select> </form> <h2><span id="firstname"></span> <span id="lastname"></span></h2> <span id="job"></span> <div style="text-align: right"> <span id="age_text"></span> <span id="age"></span> <span id="hometown_text"></span> <span id="hometown"></span> </div> </body> </html> The JavaScript named responsexml.js: var xmlHttp function showUser(str) { 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=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { 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; } } function GetXmlHttpObject() { var objXMLHttp=null if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } return objXMLHttp } And the PHP named responsexml.php: EDIT: Whoah...Is that an error with ' I see? I copied this straight off the site. EDIT: I recopied it and its still there even though there's a ' in the code when i post it <?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); ?> And the Table they use, which I made in MySQL under "ajax_demo" database with "user" for the tablename. I also made a user called "peter" with the password "abc123": id FirstName LastName Age Hometown Job 1 Peter Griffin 41 Quahog Brewery 2 Lois Griffin 40 Newport Piano Teacher 3 Joseph Swanson 39 Quahog Police Officer 4 Glenn Quagmire 41 Quahog Pilot Quote Link to comment Share on other sites More sharing options...
haku Posted August 14, 2008 Share Posted August 14, 2008 Ahh, the getElementsByTagName('hometown') wasn't wrong. Your problem lies here: echo <?xml version="1.0" encoding="ISO-8859-1"?> <person>'; You can't echo out XML documents like that, and for a very good reason - the xml closing tag is the same as the php closing tag ( ?> ). So when the php gets to the end of the xml declaration, php ends. You can close the php tag before the xml declaration, output the xml declaration as is without using an echo statement, then reopen a php tag underneath it and start your echo statement again. Quote Link to comment Share on other sites More sharing options...
Davidammit Posted August 16, 2008 Author Share Posted August 16, 2008 I think I just got my mind blown a little. Can you show an example? Quote Link to comment Share on other sites More sharing options...
Davidammit Posted August 16, 2008 Author Share Posted August 16, 2008 I'm going to refine my question a little. Do i even need the <?xml version="1.0" encoding="ISO-8859-1"?> ? Will java still be able to find the tag names on the other end? Quote Link to comment Share on other sites More sharing options...
Davidammit Posted August 16, 2008 Author Share Posted August 16, 2008 LOL...as several hours later i get what you mean. <?php blablabla?> <?xml version="1.0" encoding="ISO-8859-1"?> <?php blablablablabla?> Quote Link to comment Share on other sites More sharing options...
haku Posted August 18, 2008 Share Posted August 18, 2008 Exactly. Sorry about the late response, but I'm glad you got it figured out. Quote Link to comment Share on other sites More sharing options...
Davidammit Posted August 18, 2008 Author Share Posted August 18, 2008 Thank you for your assistance Haku. Although I ended up using responseText because I couldn't access the elements for some reason. And now I am faced with another issue. Man this stuff can be confusing...But its so fun Quote Link to comment Share on other sites More sharing options...
abitlikehomer Posted January 9, 2010 Share Posted January 9, 2010 Ahh, the getElementsByTagName('hometown') wasn't wrong. Your problem lies here: echo <?xml version="1.0" encoding="ISO-8859-1"?> <person>'; You can't echo out XML documents like that, and for a very good reason - the xml closing tag is the same as the php closing tag ( ?> ). So when the php gets to the end of the xml declaration, php ends. You can close the php tag before the xml declaration, output the xml declaration as is without using an echo statement, then reopen a php tag underneath it and start your echo statement again. Can you give me an example of how it should be done then if you can't echo comments out like that? I did what the original poster has done - copied it straight from the W3Schools website but it won't work for me. It looks like it is the closing tag like you say but I don't understand how to get around that. Thanks. 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.