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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化