How to keep the selected onchange dropdown list value after submit
04:03 26 Jan 2017

I have three dropdown list which is country,state and city. At first, the country dropdown would be displayed with all countries. When a country would be chosen, the respective states would be fetched from the MySQL database and appear in the states dropdown. Alike when a state would be chosen, the respective cities will be fetched from the MySQL database and appear in the cities dropdown.

Below is the default display before I select country,state,city and click submit button.

enter image description here

After I select country, state, city and click submit button like below. It will refresh and go back to the default display.

enter image description here

So how can I keep the selected value(United Kingdom,England,London) display in the dropdown list instead it jump back to default display after clicked submit button?

Index.php









query("SELECT * FROM countries WHERE status = 1 ORDER BY country_name ASC"); //Count total number of rows $rowCount = $query->num_rows; ?>

ajaxData.php

query("SELECT * FROM states WHERE country_id IN (".$_POST['country_id'].")");

//Count total number of rows
$rowCount = $query->num_rows;

//Display states list
if($rowCount > 0){
    echo '';
    while($row = $query->fetch_assoc()){ 
        echo '';
    }
}else{
    echo '';
}
}

if(isset($_POST["state_id"]) && !empty($_POST["state_id"])){
//Get all city data
$query = $db->query("SELECT * FROM cities WHERE state_id IN(".$_POST["state_id"].")");

//Count total number of rows
$rowCount = $query->num_rows;

//Display cities list
if($rowCount > 0){
    echo '';
    while($row = $query->fetch_assoc()){ 
        echo '';
    }
}else{
    echo '';
}
}
?>

dbConfig.php

connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
javascript php jquery html mysql