duanhan9334 2017-04-25 13:16
浏览 26
已采纳

使用Ajax的动态列表

my website is comugg.com. Im trying to make a dynamic dropdown according to this portion of my website.

https://gyazo.com/bc68919205d61b2022543d1aef81ca36

Anyways i got the first and 2nd level to work. Im struggling on my third level. i've narrowed the problem down to these two areas within my code. If anyone can give me a second eye on this i would appreciate it.

        <div id="model">
    <label>Model</label>
        <select name="model" id="modellist" onchange="getId2(this.value);">
            <option value="">Select Model</option>
        </select>
    </div>

            <div id="year">
    <label>Year</label>
        <select name="year" id="yearlist" >
            <option value="">Select Year</option>
        </select>
    </div>


    <script>
        function getId(val){
                jQuery.ajax({
                method: "POST",
                url: "getdata.php",
                data: "make="+val,
                success:function(data){
                    jQuery("#modellist").html(data);
                }
                });


        }
        function getId2(val){
                jQuery.ajax({
                method: "POST",
                url: "getdata2.php",
                data: "model="+val,
                success:function(data){
                    jQuery("#yearlist").html(data);
                }
                });

        }

    </script>

and the next part of the getdata2.php is

<?php
include_once "connection.php";
$make = $_POST["year"];


if (!empty($_POST["year"])) {
    $make = $_POST["year"];
    echo $query = "SELECT Distinct Year FROM websitemasterlist where Model='".$model."'"; 
    $results = mysqli_query($conn, $query);
    ?>
    <option value="">Select Year</option>
    <?php
    foreach ($results as $info){
    ?>
    <option value="<?php echo $info["Year"]; ?>"><?php echo $info["Year"]; ?></option>
    <?php
    }
}
?>

any help would be appreciated thank you.

  • 写回答

2条回答 默认 最新

  • duanjie9630 2017-04-25 13:41
    关注

    i do not see any year post data so update this code , hope it will work, in ajax code you use post data as

    data: "model="+val,
    

    so in your php code need to use same post value

    if (!empty($_POST["model"])) {
        $make = $_POST["model"];
        echo $query = "SELECT Distinct Year FROM websitemasterlist where Model='".$make."'"; 
        $results = mysqli_query($conn, $query);
        ?>
        <option value="">Select Year</option>
        <?php
        foreach ($results as $info){
        ?>
        <option value="<?php echo $info["Year"]; ?>"><?php echo $info["Year"]; ?></option>
        <?php
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能