0
我在我的网站中显示了此页,我在该数据库中显示了表中的所有数据。我想要的是有一个下拉列表,并从下拉列表中选择我想查看的类别,但我想选择类别而不是到任何按钮,只需选择类别即可。 这是我的网页看起来像......无需点击按钮即可从下拉列表中获取数值
而且我的代码,这怎么...
<!DOCTYPE html>
<?php
include_once 'header.php';
?>
<html>
<head>
<script>
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.php"+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
Select Category :<select name="users" onchange="showUser(this.value)">
<option value="*">All</option>
<!--emfanizei tis epiloges gia ta specialties me basi auta p exoume sti basi mas -->
<?php
$sql = mysql_query("SELECT name FROM disease");
while ($row = mysql_fetch_array($sql)){
echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
?>
</select><br><br />
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
而且getuser.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
$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT author,category,subject,content FROM announcements";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th><b>Author</b></th>
<th><b>Category</b></th>
<th><b>Subject</b></th>
<th><b>Content</b></th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . $row['content'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
你可以通过javascript过滤它们,或者只是在列表的onchange事件上发送请求的另一个查询 – albanx 2015-04-05 21:06:32
所以...你想绑定到'select'元素的'change'事件?什么东西阻止你? – David 2015-04-05 21:07:41
@大卫这样做:P和我不知道如何:P – DogFace 2015-04-05 21:08:19