December 30, 201213 yr Hello all, my first time working with XML using PHP and JS to retrieve the information. My only problem is that I just can't seem to get it to be working. Whenever I try to push the information into my HTML I get javascript console errors stating that the tagname isn't defined and thus can't get the data. Anyway I'll post the code but it would be good if you know of any guides/tutorials when working with XML. Oh also this is a very basic mailing client that I'm building so I can start to understand AJAX and such so please don't post complexities and such like security for usernames and such. That kind of stuff I can fix later. Thank you. <!doctype html> <html> <head> <meta charset="utf-8"> <title>Inbox</title> </head> <body> <div> <b>Sender:</b><span id="sender"></span><br /> <b>Subject:</b><span id="subject"></span><br /> <b>date:</b><span id="date"></span> </div> <script> if (window.XMLHttpRequest) { //IE7+, Firefox, Chrome, Opera, Safari xmlhttp= new XMLHttpRequest(); } else { //IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","php/checkmail1.php?userID=admin",true); xmlhttp.send(); xmlDoc=xmlhttp.response.XML; document.getElementById("sender").innerHTML= xmlDoc.getElementsByTagName("sender")[0].childNodes[0].nodeValue; </script> </body> </html> Whenever this is run js console states tagname sender isn't defined. So the xmlDoc isn't returning the XML data for it to read? and PHP <?php header('Content-Type: text/xml'); header("Cache-Control: no-cache, must-revalidate"); $dbhost = 'localhost'; $dbuser = 'blahdeblah'; $dbpass = ''; $dbname = 'test'; $dbtable = 'mail'; $q=$_GET["userID"]; $con = mysql_connect($dbhost, $dbuser, $dbpass); if (!$con) { die('Could not connect: ' . mysql_error()); } $dbselect = mysql_select_db($dbname,$con); $sql="SELECT * FROM $dbtable WHERE userID='$q'"; $result = mysql_query($sql); echo '<?xml version="1.0" encoding="ISO-8859-1"?><message>'; while($row = mysql_fetch_array($result)) { echo "<mail>"; echo "<userid>".$row['userID']."</userid>"; echo "<sender>".$row['Sender']."</sender>"; echo "<mailid>".$row['mailID']."</mailid>"; echo "<subject>".$row['Subject']."</subject>"; echo "<message>".$row['Message']."</message>"; echo "<date>".$row['Date']."</date>"; echo "<time>".$row['Time']."</time>"; echo "<status>".$row['status']."</status>"; echo "</mail>"; } echo "</message>"; mysql_close($con); ?>
December 30, 201213 yr Author copy and paste the generated response .... Heres what I get when I just simply use document.write(xmlDoc); Sender: Subject: date: undefined <script> if (window.XMLHttpRequest) { //IE7+, Firefox, Chrome, Opera, Safari xmlhttp= new XMLHttpRequest(); } else { //IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","php/checkmail1.php?userID=admin",true); xmlhttp.send(); xmlDoc=xmlhttp.response.XML; document.write(xmlDoc); </script> Heres the output from when I use: document.getElementById("sender").innerHTML= xmlDoc.getElementsByTagName("sender")[0].childNodes[0].nodeValue Sender: Subject: date: Edited December 30, 201213 yr by BITmixit
Create an account or sign in to comment