4

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 Answers 2

3

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

It should be

$("#cityList").html(data);
Sign up to request clarification or add additional context in comments.

1 Comment

hey satya! cant thank u enough man...ya forgot to add "" in cityList...What an idiot...it works now...Thanks a million
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>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.