Dynamic dropdown list with PHP & MYSQL

时间:2016-10-20 12:42:14

标签: php jquery html mysql dropdown

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:

Code for dynamic dropdown form :

<?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>

Database connection code :

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

Code for 2nd dynamic dropdown :

<?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.

2 个答案:

答案 0 :(得分:1)

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

It should be

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

答案 1 :(得分:0)

I think your problem might be here:

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

Try and use this instead:

foreach ($results as $country){
echo'<option value="'.$country["cid"].'">'.           
$country["country"].'</option>';
}