我正在尝试显示数据onselect方法但数据未显示。 这是代码。
的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>
和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);
?>
我猜getuser.php?q="+str
没有通过。
请帮忙,因为我是ajax的新手。
答案 0 :(得分:1)
如果你在getuser.php中传递id,如:
getuser.php Q = 1
你应该传递参数,如:
$q = $_GET['q'];
并查询数据库
$sql="SELECT * FROM STUDENTS WHERE id = '".$q."'";
答案 1 :(得分:0)
简单地传递不会得到你需要的变量
$q=$_GET['q'];
在您的查询页面中。 然后再试一次。如果仍然无法正常工作,请尝试回显您的mysql查询并检查是否在那里获得$ q的值。