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