如何在php,mysqli中保存两个动态相关选择框的值

时间:2017-07-10 14:48:19

标签: javascript php ajax

我正在构建一个名为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>";

}

?>

This is my output

在更改状态时,它不会处理选择框中的列表区域 控制台显示错误

注意:未定义的索引:存储在第5行的C:\ xampp \ htdocs \ ifsctech \ ajax.php

问题是商店的选定值无法用于下一个要处理的查询。如何保存和发送两个选择框的动态值来处理查询请帮忙

2 个答案:

答案 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'

需要考虑的一些要点:

  • 对于localhost,您的数据库应具有所有相关表 (在我的例子中,db = state_city和tb = state,tb = city)。如果你,它不会工作 在localhost上有不同的数据库。
  • 但是,这种关系适用于不同的Apache Hosting Server 数据库也是如此。
  • tb = state [columns = state_id,state_name] =&gt;主键:state_id
  • tb = city [columns = city_id,state_id,city_name] =&gt;主键:
    city_id
相关问题