Dear All,
I am currently working on an assignment. And I wanted to implement a bit ajax in it.
I've got my html page, and on an onclick event the ajax code is going to populate a table and place it in my html page. The php file is working correctly but there's a problem with the java-script codes.
Can anyone of you please figure out what the error is.
var xmlhttp;
function showUserName(){
var fname= document.getElementById("search_fname");//alert("fname is " + fname);
var lname= document.getElementById('search_lname');//alert("lname is "+ lname);
if(window.XMLHttpRequest){
//code for ie7+,firefox,chrome,opera,safari
xmlhttp=new XMLHttpRequest();
}
else{
//code for ie6,5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlhttp==null){
alert("cannot create XMLHTTP instance");
return false;
}
var url="searchUser.php";
url=url+"?search_fname="+fname+"&search_lname="+lname;
xmlhttp.onreadystatechange=theResponse;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function theResponse(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
var tableName="<table><tr><th>First Name</th><th>Last Name</th><th>Email</th></tr>";
var xmlDoc=xmlhttp.responseXML.documentElement;//refers to root position of the xml <Nom>
var NomNo=xmlDoc.childNodes.length;
for(var i=0;i<NomNo;i++){
var namenode=xmlDoc.childNodes[i];//refers to Nom/Name
tableName=tableName+"<tr>";
var nameNo=namenode.childNodes.length;//refers to no of element under Name
for(var j=0;j<nameNo;j++){
var Nam=namenode.childNodes[j];//1st object
var sibl=Nam.childNodes[0];//its value
if(sibl!=null){
tableName=tableName+"<td>"+sibl.nodeValue+"</td>";
}
else{
tableName=tableName+"<td> </td>";
}
tableName=tableName+"</tr>";
}
tableName=tableName+"</table>";
document.getElementById("div_searchname").innerHTML=innerHTML+tableName;
}
}
}
else{
alert("there was a problem with the request");
}
}
The php file
<?php
$fname="a";//$_GET('search_fname');echo $fname;
$lname="a";//$_GET('search_lname');
include("dbConnect.php");
mysql_select_db("idareyou",$con);
//echo "<br/>line just after include dbconnect";
//echo $fname;
//$sql="SELECT fname,lname,email FROM user WHERE fname=$fname AND lname=$lname";
$result=mysql_query("SELECT fname,lname,email FROM user WHERE fname='$fname' AND lname='$lname' ");
//$result=mysql_query( "SELECT fname,lname,email FROM user WHERE".'"'.fname.'"'.=.'"'.$fname.'"'.
$numRows=mysql_num_rows($result);//echo $numRows;
if($numRows<1){
mysql_close($con);
echo "No result found";
}
header('Content-Type: text/xml');
$xml_name = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xml_name = $xml_name."<Nom>" ;
while($rows=mysql_fetch_array($result) ){
$xml_name=$xml_name."<Name>";
$xml_name=$xml_name."<fname>".$rows["fname"]."</fname>";
$xml_name=$xml_name."<lname>".$rows["lname"]."</lname>";
$xml_name=$xml_name."<email>".$rows['email']."</email>";
$xml_name=$xml_name."</Name>";
}
$xml_name=$xml_name."</Nom>";
echo $xml_name;
mysql_close($con);
?>