Jump to content

Ajax-reosponsexml


pritam79
 Share

Recommended Posts

I am trying this example from w3schools, but it’s not working at all.This is ‘AJAX-ResponseXML.htm’-

<html><head><script type="text/javascript" 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>

This is ‘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)  {  xmlDoc=xmlhttp.responseXML;  document.getElementById("Firstname").innerHTML=  xmlDoc.getElementsByTagName("Firstname")[0].childNodes[0].nodeValue;  document.getElementById("Lastname").innerHTML=  xmlDoc.getElementsByTagName("Lstname")[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(){if (window.XMLHttpRequest)  {  // code for IE7+, Firefox, Chrome, Opera, Safari  return new XMLHttpRequest();  }if (window.ActiveXObject)  {  // code for IE6, IE5  return new ActiveXObject("Microsoft.XMLHTTP");  }return null;}

And this is- ‘responsexml.php ‘

<?php$q=$_GET["q"];$con = mysql_connect('localhost', 'root', '');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);?>

In localhost I on clicking the html file I get a dropdown box where from I get to select the options but on doing so I do not get any output. I have created the database as well. What could be the reason? Please help.

Link to comment
Share on other sites

You have several mistakes:

  • The XML is badly formed:
    <?xml version="1.0" encoding="ISO-8859-1"<person>'

  • XML is case sensitive. This will not work:getElementsByTagName("Firstname")<firstname>getElementById("Firstname")<span id="firstname">

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...