/    Sign up×
Community /Pin to ProfileBookmark

Cant retrieve data from db

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>[/CODE]

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;
}
[/CODE]

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);
?>
[/CODE]

Thanks in advance!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@yuehyihauthorApr 24.2009 — bump
×

Success!

Help @yuehyih spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...