How to display MYSQL record using a combobox in PHP/AJAX?
Need help. I using 3 tables to store booking information: eventinfo
, customer
and testbook
and I try to display records by using combo box to sort by username and eventID.
Problem is the combobox does not load data from database.
My code:
index.php
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4./jquery.min.js"></script>
<script type="text/javascript">
function showUser(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.开发者_如何学Pythonphp?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option selected="selected">--Select user--</option>
<?php
include('db.php');
$sql=mysql_query("select username, custNo from testbook");
while($row=mysql_fetch_array($sql))
{
$id=$row['custNo'];
$username=$row['username'];
echo '<option value="'.$id.'">'.$username.'</option>';
} ?>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', '', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("eventdb", $con);
$sql="SELECT * FROM testbook WHERE custNo = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Booth</th>
<th>Event</th>
<th>Date</th>
<th>Status</th>
<th>Booking ID</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['boothAlias'] . "</td>";
echo "<td>" . $row['eventID'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['bstatus'] . "</td>";
echo "<td>" . $row['bookingID'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Output:
You need to use $_GET
... You must get the "q" on getuser.php?q=""
...
So,therefore your query is:
$sql = "SELECT * FROM testbook WHERE custNo = '".$_GET['q']."' ";
精彩评论