PHP&的动态下拉列表MySQL数据库 [英] Dynamic dropdown list with PHP & MYSQL

查看:101
本文介绍了PHP&的动态下拉列表MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个具有动态下拉列表的表单,以从MYSQL提取数据.我的数据库很好,没有错误.

I have been trying to create a form with dynamic dropdown list fetching data from MYSQL. My database is fine without errors.

第一类下拉列表工作正常,但我想知道为什么第二个下拉列表无法正常工作.我只是无法跟踪代码中的任何错误,但这正在发生.这是我的代码:

The first category of dropdown is working fine but I am wondering why my 2nd dropdown is not working. I just cant trace any error in the code and yet this is happening. here's my code:

动态下拉列表的代码:

<?php
    include_once "connection.php";
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Dropdown Ajax</title>
    </head>
    <body>
        <div class="country">
            <label>Country</label>
            <select name="country" onchange="getId(this.value);">
                <option value="">Select Country</option>
                //populate value using php
                <?php
                    $query = "SELECT * FROM country";
                    $results=mysqli_query($con, $query);
                    //loop
                    foreach ($results as $country){
                ?>
                        <option value="<?php echo $country["cid"];?>"><?php echo $country["country"];?></option>
                <?php
                    }
                ?>
            </select>
        </div>

        <div class="city">
            <label>City</label>
            <select name="city" id="cityList">
                <option value=""></option>
            </select>
        </div>
    <script   src="https://code.jquery.com/jquery-3.1.1.js"   integrity="sha256-
16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="   crossorigin="anonymous">  
    </script>
    <script>
        function getId(val){
            //We create ajax function
            $.ajax({
                type: "POST",
                url: "getdata.php",
                data: "cid="+val,
                success: function(data){
                    $(#cityList).html(data);
                }
            });
        }
    </script>
    </body>
</html>

数据库连接代码:

<?php
    $con = mysqli_connect("localhost", "root", "kensift", "tuts");
    //Check connection
    if(mysqli_connect_errno()){
        echo "Failed to connect:".mysqli_connect_errno();
    }
?>

第二个动态下拉菜单的代码:

<?php
    include_once "connection.php";
    if (!empty($_POST["cid"])) {
        $cid = $_POST["cid"]; 
        $query="SELECT * FROM city WHERE cid=$cid";
        $results = mysqli_query($con, $query);

        foreach ($results as $city){
?>
            <option value="<?php echo $city["cityId"];?>"><?php echo $city["city"];?>
    </option>       
<?php
        }
    }
?>  

这三个代码部分位于不同的文件中.

These three code parts are in different files.

推荐答案

我认为您的代码正确,只是忘记了引号id"#cityList".

I think your code is correct except forgot the quotations id "#cityList" .

应该是

$("#cityList").html(data);

这篇关于PHP&amp;的动态下拉列表MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆