我正在构建一个名为store locator的新Web应用程序,我需要通过相关的选择框过滤我的sql数据,并在表中显示store的地址。问题是我无法同时保存sql查询的两个选择框的值
这是我的index.php代码
<?php
require 'dbconn/dbconfig.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title>store SEARCH </title>
</head>
<body>
<form name="form1" action="" method="post">
<table>
<tr>
<td>Select store</td>
<td>
<select id="storeid" onchange="change_store()">
<option>Select store</option>
<?php
$res=mysqli_query($db,"SELECT DISTINCT `STORE` FROM `master` ORDER BY `STORE` ASC");
while ($row=mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row['STORE']; ?>"><?php echo $row['STORE'];?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Select State</td>
<td>
<div onchange="change_state()">
<select id="statedd" >
<option>select State</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select District </td>
<td>
<div onchange="change_district()">
<select id="districtdd">
<option>select District</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select City </td>
<td>
<div onchange="change_city()">
<select id="citydd">
<option>select City</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select Branch </td>
<td>
<div>
<select id="branchdd">
<option>select Branch</option>
</select>
</div>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function change_store() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?store="+document.getElementById("storeid").value,false);
xmlhttp.send(null);
document.getElementById("statedd").innerHTML=xmlhttp.responseText;
}
function change_state() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?state="+document.getElementById("stateid").value,false);
xmlhttp.send(null);
document.getElementById("districtdd").innerHTML=xmlhttp.responseText;
alert(xmlhttp.responseText);
}
function change_district() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?district="+document.getElementById("districtid").value,false);
xmlhttp.send(null);
document.getElementById("citydd").innerHTML=xmlhttp.responseText;
}
function change_city() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","ajax.php?city="+document.getElementById("cityid").value,false);
xmlhttp.send(null);
document.getElementById("branchdd").innerHTML=xmlhttp.responseText;
}
</script>
</body>
</html>
这是我的ajax.php
<?php
include 'dbconn/dbconfig.php';
?>
<?php
$store=$_GET["store"];
$state=$_GET["state"];
if($store!='')
{
$res=mysqli_query($db, "SELECT DISTINCT `STATE` FROM `master` WHERE `STORE`='$store' ORDER BY `STATE` ASC");
echo "<select id='stateid' onchange='change_state()'>";
while ($row=mysqli_fetch_array($res)) {
echo "<option value=".$row['STATE'].">".$row['STATE']."</option>";
}
echo "</select>";
}
if($state!='')
{
$res=mysqli_query($db, "SELECT DISTINCT `DISTRICT` FROM `master` WHERE `STATE` LIKE '$state%' AND `STORE`='$store' ORDER BY `DISTRICT` ASC");
echo "<select id='districtid' onchange='change_district()'>";
while ($row=mysqli_fetch_array($res)) {
echo "<option value=".$row['DISTRICT'].">".$row["DISTRICT"]."</option>";
}
echo "</select>";
}
?>
在更改状态时,它不会处理选择框中的列表区域 控制台显示错误
注意:未定义的索引:存储在第5行的C:\ xampp \ htdocs \ ifsctech \ ajax.php
问题是商店的选定值无法用于下一个要处理的查询。如何保存和发送两个选择框的动态值来处理查询请帮忙
答案 0 :(得分:0)
这似乎是问题
xmlhttp.open("GET","ajax.php?store="+document.getElementById("storeid").value,false);
主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用。如需更多帮助,请查看this。
答案 1 :(得分:0)
即使我正在努力解决同样的问题。我发现的内容也可能对您有所帮助。我将分享对我有用的东西。你可以参考它。
<?php
//fetch.php
$connect = mysqli_connect("localhost", "root", "", "state_city");
$output = '';
$sql = "SELECT * FROM city WHERE state_id = '".$_POST["stateId"]."' ORDER BY city_name";
$result = mysqli_query($connect, $sql);
$output = '<option value="">Select City</option>';
while($row = mysqli_fetch_array($result))
{
$output .= '<option value="'.$row["city_id"].'">'.$row["city_name"].'</option>';
}
echo $output;
?>
PHP代码
type: 'POST'
需要考虑的一些要点: