duanhan9334
2017-04-25 13:16
浏览 25

使用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
        }
    }
    ?>
    
    打赏 评论
  • doulu6234 2017-04-25 13:23

    Turns out that my variables were in the wrong area.

    打赏 评论

相关推荐 更多相似问题