Reputation: 325
I am trying to display the data onselect method but the data is not getting displayed. This is the code.
index.html
<html>
<head>
<script>
function showUser(str)
{
alert(str);
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">MEGHALI</option>
<option value="2">SONY</option>
<option value="3">DHANU</option>
<option value="4">ADITYA</option>
<option value="5">MAHESH</option>
<option value="6">NEERAJ</option>
<option value="7">VINEET</option>
<option value="8">ASHWINI</option>
<option value="9">SANTOSH</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
and the getuser.php
<?php
$q = intval($_GET['q']);
$con = mysqli_connect("localhost", "root", " ", "flowers");
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"FLOWERS");
$sql="SELECT * FROM STUDENTS WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>ID</th>
<th>NAME</th>
<th>CITY</th>
<th>PHONE</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['CITY'] . "</td>";
echo "<td>" . $row['PHONE'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I guess the getuser.php?q="+str
is not getting passed.
Please help as I am newbie to ajax.
Upvotes: 0
Views: 97
Reputation: 6519
Simply passing doesn't get the variable there you need to get it by
$q=$_GET['q'];
in your query page. and try again. if still it is not working try echoing your mysql query and check whether you are getting the value of $q there.
Upvotes: 0
Reputation: 484
if you are passing id in getuser.php like:
getuser.php?q=1
you should get passed parameter like :
$q = $_GET['q'];
and query the database
$sql="SELECT * FROM STUDENTS WHERE id = '".$q."'";
Upvotes: 1