Dependent dropdown example in PHP, MySQL

dependent_dropdownIf you wanted to change the another select box value on the change of the first select box, Here is the answer of your question.

I'm writing an article on dependent select box in PHP, MySQL and Ajax. If you change the first drop down value then second drop down options will get change according to first drop down.

Here I'm explaining an example of dependent select box like if you change the country name 'India' then in second select box India's States will be populated and so on.

Click here to download demo code





Share This:

18 thoughts on “Dependent dropdown example in PHP, MySQL

  1. sk

    Hi , i am trying this code but it is not working.
    I am trying to create dependent dropdown where a person can select a particular shop depending on a region like south, north, east, west.
    I have created a database where 4 columns are present like id,region_id, region, store name. I have 2 dropdowns one shows the region and another shows store name. Both are called from the database. Region dropdown is showing all regions.but store name is blank.
    I am trying to fetch stores by region id. if region_id==' ' then it will call select query where region_id gets a match. I need help if I am doing anything wrong. That's why I am not getting the results.




    echo "" . $row['region'] . "";


    Store Name

    Select Store


    query('select * from store_details where region_id='.$region_id.'');
    $options = "Select store";
    while($row = $states_result->fetch_assoc())
    $options .= "".$row['name']."";
    echo $options;

    Ajax Code:

    $('#region').on('change', function(){
    var region_id = this.value;
    type: "POST",
    url: "SearchRegion.php",
    success: function(result){

    I have checked DbConnection.php file it is working fine. Just the issue is this:[enter image description here][1]

    Store Name dropdown is blank. and i am not getting any errors.

  2. Ashutosh

    what does $country_id specifies does it tells us the country_id column of database table or it tells us about the new variable declaration???

  3. Ashutosh

    Ship Country

    Ship State

    Select State

    $('#ship_country').on('change', function(){
    var country_id = this.value;
    type: "POST",
    url: "states.php",
    success: function(result){

    and my states.php file

    $country_id = mysqli_real_escape_string($mysqli,$_REQUEST['country_id']);
    $states_result = $mysqli_query($mysqli,"select * from state where country_id='.$country_id.'");
    $options = "Select State";
    echo "hi";
    while($row = $states_result->fetch_assoc()) {
    $options .= "".$row['state_name']."";
    echo $options;

    help me out plzzzz

  4. Mayur

    Show countries but after select countries states is not show
    codding error fix
    in line change
    $country_id = mysql_real_escape_string($_POST['country_id']); is wrong line to change
    $country_id = $_POST['country_id'];

  5. tony corix

    Hi. Thank you so much for this code!

    This worked for me but I had to change line 3 in get_states.php to:
    $country_id = mysqli_real_escape_string($conn,$_POST['country_id']);

    old value:
    $country_id = mysql_real_escape_string($_POST['country_id']);

    Cheers! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *