ajax_example1.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function showdetail(id)
{
//var xmlhttp;
//if(window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
/*else if(window.ActiveXObject)
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");*/
xmlhttp.open("GET","data_fetch.php?id="+id,true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 || xmlhttp.readyState=="complete")
{
//if(xmlhttp.status==200)
{
//if(xmlhttp.responseText!='')
{
//document.getElementById('emp_nm').innerHTML=xmlhttp.responseText;
xmldoc=xmlhttp.responseXML;
document.getElementById("emp_nm").innerHTML=xmldoc.getElementsByTagName("emp_nm")[0].firstChild.data;
document.getElementById("dpt_nm").innerHTML=xmldoc.getElementsByTagName("dpt_nm")[0].firstChild.data;
document.getElementById("wrkphour").innerHTML=xmldoc.getElementsByTagName("wrkphour")[0].firstChild.data;
document.getElementById("sal").innerHTML=xmldoc.getElementsByTagName("sal")[0].firstChild.data;
document.getElementById("ph_no").innerHTML=xmldoc.getElementsByTagName("ph_no")[0].firstChild.data;
}
}
}
}
}
</script>
</head>


<body>
<form name="form1" method="post" action="">
<select name="select_name" size="1" id="select_name" onChange="showdetail(this.value);">
<?php
$conn=mysql_connect('localhost','root','') or die("Couldn't connect to server.");
mysql_select_db('developer',$conn) or die("Couldn't connect to database");
$sql="select emp_id from emp_detail";
$res=mysql_query($sql) or die("Couldn't execute query.");
while($arr=mysql_fetch_array($res))
{
echo "<option value=$arr[0]>$arr[0]</option>";
}
mysql_close($conn);
?>
</select>
</form>
<div id='emp_nm'></div>
<div id='dpt_nm'></div>
<div id='wrkphour'></div>
<div id='sal'></div>
<div id='ph_no'></div>
</body>
</html>

and other file i.e data_fetch.php as

<?php
$id=$_GET;
$conn=mysql_connect('localhost','root','') or die("Couldn't connect to database.");
mysql_select_db('developer',$conn) or die("Couldn't select database.");
$sql="select * from emp_detail where emp_id='$id'";
$res=mysql_query($sql) or die("Couldn't execute query");
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<emp>";
while($arr=mysql_fetch_array($res))
{
//echo "<empid>".$arr[0]."</empid>";
echo "<emp_nm>".$arr[1]."</emp_nm>";
echo "<dpt_nm>".$arr[2]."</dpt_nm>";
echo "<wrkphour>".$arr[3]."</wrkphour>";
echo "<sal>".$arr[4]."</sal>";
echo "<ph_no>".$arr[5]."</ph_no>";
/*echo $arr[1];
echo $arr[2];
echo $arr[3];
echo $arr[4];
echo $arr[5];*/
}
echo "</employee>";
mysql_close($conn);
?>

But i dont get any output and get error as

xmldoc has no properties
document.getElementById("emp_nm").innerHTML=xmldoc.getElementsByTagName("em...

please send me error...

To find out if an id is being passed, can you test the data_fetch.php page using a sample id in the query string? In other words type the url into the browser and include an id that you know is in the database. Like this:
http://www.domainname.com/data_fetch.php?id=23

This will at least tell you if the problem is in the data_fetch.php page.
Also you need a where clause in your $sql query.
"$sql="select emp_id from emp_detail";"

If you're using Firefox and have Firebug installed (which you likely do) look under the "Console" tab and check the response from the server. If it's empty or the response code isn't 200 something's wrong on the server side. If not, you have an error in your Javascript (Firebug will probably let you know where it is).

One more thought -- try adding:

header('Content-type: text/xml');

to the top of data_fetch.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.