Javascript 如何基于两个下拉选择从数据库检索数据
我已经使用ajax对一个下拉选择进行了数据检索,但我希望它根据两个下拉选择条件检索数据。如何将两个变量解析到showChoice函数中,然后将第二个选项存储到另一个变量中。我将感谢任何帮助Javascript 如何基于两个下拉选择从数据库检索数据,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我已经使用ajax对一个下拉选择进行了数据检索,但我希望它根据两个下拉选择条件检索数据。如何将两个变量解析到showChoice函数中,然后将第二个选项存储到另一个变量中。我将感谢任何帮助 <html> <head> <script> function showChoice(str) { if (str == "") { document.getElementById("txtHint").inner
<html>
<head>
<script>
function showChoice(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","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="choices" onchange="showChoice(this.value)">
<option value="">Select a departure point:</option>
<option value="London">London</option>
<option value="New Castle">New Castle</option>
</select>
</form>
</body>
</html>
功能选择(str){
如果(str==“”){
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}否则{
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}否则{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“getuser.php?q=“+str,true”);
xmlhttp.send();
}
}
选择出发点:
伦敦
纽卡斯尔
然后是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
$q = $_GET["q"];
$con = mysqli_connect('localhost','root','','busdb');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"busdb");
$sql="SELECT * FROM busRoutes WHERE Departure = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>ID</th>
<th>Departure</th>
<th>Destination</th>
<th>Time</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Departure'] . "</td>";
echo "<td>" . $row['Destination'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
桌子{
宽度:100%;
边界塌陷:塌陷;
}
表,td,th{
边框:1px纯黑;
填充物:5px;
}
th{文本对齐:左;}
您可以这样使用,看看。如果您遗漏了这一点,可能需要包括jquery库
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
html将如下所示
function showChoice(str,sel2) {
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","getuser.php?q="+str+"&q2="+sel2,true);
xmlhttp.send();
}
}
$(document).ready(function()
{
$('.select').change(function()
{
var sel1=$('#select1').val();
var sel2=$('#select2').val();
showChoice(sel1,sel2);
})
});
</head>
<body>
<form>
<select id="select1" class="select" name="choices">
<option value="">Select a departure point:</option>
<option value="London">London</option>
<option value="New Castle">New Castle</option>
</select>
<select id="select2" class="select" name="choices2">
<option value="">Select a departure point:</option>
<option value="London">London</option>
<option value="New Castle">New Castle</option>
</select>
</form>
</body>
</html>
选择出发点:
伦敦
纽卡斯尔
选择出发点:
伦敦
纽卡斯尔
更新
这是
更新2
getuser.php
<?
echo $q=$_GET['q'];
echo $q2=$_GET['q2'];
?>
我感谢您的帮助,我更新了我的sql select语句如下:$sql=“select*FROM busRoutes WHERE REFEATION='”$q.”和DESTITION='“$q2.””;但似乎什么也没有发生,甚至没有错误。你能解释一下$(dcoment).ready(function()下的代码以及为什么要添加它吗?你是否在你的php文件中添加了$q2=$\u GET[“q2”];$(dcoment).ready(function())只是一个jquery事件。下面是完整的描述是的,我做了。似乎只有第一个下拉列表响应任何mysql语句应答中的scheck更新和您的txtHint div。值是否正在打印??