AJAX DropDown没有填充

时间:2016-05-02 21:08:46

标签: php jquery mysql

我正在使用Jquery和PHP。因此,在选择第一个下拉列表时,应将第一个下拉列表的值传递给Mysql查询,然后填充第二个下拉列表,但第二个下拉列表显示为空白。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("#city").change(function() {
            var value = $(this).val();
            $.ajax({
                type : "GET",
                url : 'abc.php',
                data : {
                    choice : value
                },
                success : function(data){
                    $('#123').html(data); 
                }
            })
        });
    });
</script>

<form action="" method="post">
    <select class="form-control" id="city" action="" name="city" value="">
        <option value="">--</option>
        <option value="1"</option>
        <option value="2"</option>
        <option value="3"</option>
    </select>
    <br/>
</div>
<div class="form-group">        
    <select class="form-control" action="" name="123" id="123"">
        <option value="--">--</option>
        <?php
        $query = "SELECT DISTINCT `Comm` FROM `Comm_New` WHERE `Market`='".$_GET['city']."'  ORDER BY `Comm` ASC";
        if ($result = mysqli_query($link, $query)) {
            while ($Comm = mysqli_fetch_assoc($result)) {
                print_r("<option value='".$Comm['Comm']."'>".$Comm['Comm']."</option>");
            }
        }           
        ?>
    </select><br/>
</div>

1 个答案:

答案 0 :(得分:1)

从我们在评论中的对话中,您调用的是与最初加载的页面相同的页面。这在技术上不一定是问题,它只是没有正确实施。要加载同一页面,您需要执行以下操作:

<?php
// Make sure your database is initiated above here so this can use it.
// I am going to demonstrate a basic binding using a super basic PDO
// connection because procedural mysqli_* with bind is just annoying
$link = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
// Notice that you send "choice" as the GET key in your ajax, not "city"
if(!empty($_GET['choice'])) {
?>
    <select class="form-control" action="" name="123" id="123"">
        <option value="">--</option>
        <?php
        // prepare, bind, execute here
        $query = $link->prepare("SELECT DISTINCT `Comm` FROM `Comm_New` WHERE `Market` = :0  ORDER BY `Comm` ASC");
        $query->execute(array(':0'=>$_GET['choice']));
        // PDO has a lot of connection settings where you can set the default
        // return type so you don't need to tell it to fetch assoc here.
        // Also, you would tell the the connection not to just emulate bind
        // etc.. I would consider using PDO or the OOP version of mysqli 
        while ($Comm = $query->fetch(PDO::FETCH_ASSOC)) {
            echo "<option value='".$Comm['Comm']."'>".$Comm['Comm']."</option>";
        }

?>    </select>
<?php
        // Stop the page from running further
        die();
    }
?><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("#city").change(function() {
            var value = $(this).val();
            $.ajax({
                type : "GET",
                url : 'abc.php',
                data : {
                    choice : value
                },
                success : function(data){
                    // Populate the empty container #new_drop
                    $('#new_drop').html(data); 
                }
            })
        });
    });
</script>

<form action="" method="post">
    <select class="form-control" id="city" action="" name="city" value="">
        <!--
             Your options are malformed. Missing close ">"
             probably just copy error
        -->
        <option value="">--</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select><br/>
    </div>
    <!-- Add id="new_drop" to this div -->
    <div class="form-group" id="new_drop">
    </div>

理想情况下,您希望将顶部放在新页面上,并且可能返回一组数据而不是直接html,但是ajax非常灵活。