doushi1960 2019-03-21 15:54
浏览 87
已采纳

依赖下拉列表没有填充

I am trying to populate a drop down box depending which county has been selected the second drop down should populate with the provinces of the chosen county.

I don't understand why the second dropdown is not populating. I am getting a JSON response in the console so the PHP is correct. I am sure it is something silly but I just cant see it. Thanks in advance.

index.php page

<?php
    include "config.php";
?>
<!doctype html>
<html>
    <head>
        <title>dropdown</title>
        <link href="style.css" rel="stylesheet" type="text/css">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="jquery-1.12.0.min.js" type="text/javascript"></script>

        <script type="text/javascript">
            $( document ).ready( function () {

                $( "#country" ).change( function () {
                var countryid = $( this ).val();

                    $.ajax( {
                        url: 'getUsers.php',
                        type: 'POST',
                        data: {
                            countryid: countryid
                        },
                        dataType: 'json',
                        success: function ( response ) {

                            var len = response.length;

                            $( "#province" ).empty();
                            for ( var i = 0; i < len; i++ ) {
                                var id = response[ i ][ 'provinceid' ];
                                var name = response[ i ][ 'provincename' ];

                                $( "#province" ).append( "<option value='" + id + "'>" + name + "</option>" );

                            }
                        }
                    } );
                } );

            } );
        </script>
    </head>
    <body>


        <div>Country</div>
        <select id="country">
            <option value="0">- Select -</option>
            <?php 
                // Fetch Country
                $stmt = $conn->prepare('SELECT * FROM countries');
                $stmt->execute();

                while($countries = $stmt->fetch()) {

                $countryid = $countries['id'];
                $countryname_en = $countries['countryname_en'];

                // Option
                echo "<option value='".$countryid."' >".$countryname_en."</option>";
                }


            ?>
        </select>
        <div class="clear"></div>

        <div>Province</div>
        <select id="province">
            <option value="0">- Select -</option>
        </select>
    </body>
</html>

PHP

<?php

    include "config.php";

    var_dump($_POST);
    $countryid = $_POST['countryid'];

    $countryid = "CA";

    $stmt = $conn->prepare('SELECT * FROM provincestates WHERE countryid = :countryid');
    $stmt->execute(array(
        ':countryid' => $countryid
    ));

    /*
    echo "<pre>";
    echo "prov is:" . $province_array = array();
    echo "</pre>";
    */


    while ($province = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $provinceid   = $province['provincestatecode'];
        $provincename = $province['provincestatename_en'];

        $province_array[] = array(
            "provinceid"    => $provinceid,
            "provincename"  => $provincename
        );
    }

    echo json_encode($province_array);
  • 写回答

2条回答 默认 最新

  • douwen2158 2019-03-31 21:43
    关注

    I finally got it:

    Apparently JSON.parse is depreciated and I had to change it from var json=JSON.parse( response ); to just var response = response

    Thanks for all your help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭