Hi, I trying to use AJAX to load user’s profile through a drop-down list on selected user.
There are 3 seperate files:
1) view_info.php
2) selectuser.js
3) getuser.php
I tested and found that the value $q did pass from view_info -> selectuser.js -> getuser.php, and value changes correctly as i select different user from the drop-down list.
Problem is, it simply can’t fetch data from my database using the value $q. I tried debugging it for 3 hours already and is totally out of idea whats wrong with the script. Please help me check what is wrong, i’m new to AJAX, this is my first script..
view_info.php File:
[CODE]
<?php
$con = mysql_connect(‘yuehyih.hypermartmysql.com’, ‘username’, ‘password’);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“yuehyih_ieye”, $con);
$sql=”SELECT * FROM user”;
$result = mysql_query($sql);
?>
<form>
Select a User:
<select name=”users” onchange=”showUser(this.value)”>
<?php $num=1;
while($row = mysql_fetch_array($result))
{ ?>
<option value=”<? echo $num; ?>”><?php echo $row[‘username’];?></option>
<?php
$num=$num+1;
} ?>
</select>
</form>
<p>
<div id=”txtHint”><b>User info will be listed here.</b></div>
</p>
selectuser.js File
[CODE]
var xmlHttp;
function showUser(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert (“Browser does not support HTTP Request”);
return;
}
var url=”getuser.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”)
{
document.getElementById(“txtHint”).innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}
getuser.php
[CODE]
<?php
$q=$_GET[‘q’];
echo “value of q is “.$q;
$con = mysql_connect(‘yuehyih.hypermartmysql.com’, ‘yuehyih’, ‘000099’);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“yuehyih_ieye”, $con);
/*** i have tried with sql=”SELECT * FROM user WHERE userID=’ “.$q.” ‘ “; but still didn’t work ***/
$sql=”SELECT * FROM user WHERE userID=’$q'”;
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo “<table border=’0’ width=380px> <tr><td width=50px><b>Name</b></td><td>”;
echo “:”. $row[‘Name’] . “</td></tr>”;
echo “<tr><td><b>Email</b></td><td>”;
echo “:”. $row[‘Email’];
echo “</td></tr>”;
echo “<tr><td><b>Contact</b></td><td>”;
echo “:”. $row[‘Contact’];
echo “</td></tr>”;
echo “<tr><td><b>Contry</b></td><td>”;
echo “:”. $row[‘Contry’];
echo “</td></tr>”;
echo “<tr><td><b>State</b></td><td>”;
echo “:”. $row[‘State’];
echo “</td></tr>”;
echo “<tr><td><b>Address</b></td><td>”;
echo “:”. $row[‘Address’];
echo “</td></tr>”;
echo “<tr><td><b>Zipcode</b></td><td>”;
echo “:”. $row[‘Zipcode’];
echo “</td></tr>”;
echo “</table>”;
}
mysql_close($con);
?>
Thanks in advance!