我正试图用基于用户id的mysql数据库填充表。当我使用一个只有几个用户id的选项时,它就可以工作了。但是,如果我使用并输入用户Id,它会在几秒钟内显示带有数据的表并关闭它。
<html>
<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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","../action/subs/getcalls.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="788">user 788</option>
<option value="786">User 786</option>
<option value="787">User 787</option>
<option value="789">User 789</option>
</select>
</form>
<br>
<div id="txtHint"><b></b></div>
</body>
</html>
**如果我更换
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="788">user 788</option>
<option value="786">User 786</option>
<option value="787">User 787</option>
<option value="789">User 789</option>
</select>
</form>
通过
<form>
<input name="users" onchange="showUser(this.value)"/>
</form>
表格将显示几秒钟并关闭
这里是getcall.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
include '../db/connect.php';
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM oz2ts_call_logs WHERE member_id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Device ID</th>
<th>Title</th>
<th>Note</th>
<th>Status</th>
<th>Case Number</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['device_id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['case_number'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
代码对我有效,只有当你点击输入时,onchange才适用,也许你只想从<input name="users" onchange="showUser(this.value)"/>
更改为<input name="users" onkeyup="showUser(this.value)"/>
<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
也属于头