DEV Community

Robert Look
Robert Look

Posted on

[country,state,city] dropdown list using javascript - Step by Step

Today In this tutorial, we will show you how to dynamically create a country state city dropdown list in PHP using jQuery ajax from MySQL database.

This tutorial will guide you step by step on how to create country state city in dropdown list onchange in PHP from MySQL using Ajax or populate the first, second, and third dropdown based on the first and second selection of dropdown in PHP. jquery plugin for country state city dropdown in this tutorial.

In this dependent country state city dropdown using ajax, in PHP MySQL, we will show states and cities in the dropdown list based on the selected country and state value.

<?php
require_once "db.php";
$country_id = $_POST["country_id"];
$result = mysqli_query($conn,"SELECT * FROM states where country_id = $country_id");
?>
<option value="">Select State</option>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row["id"];?>"><?php echo $row["name"];?></option>
<?php
}

Enter fullscreen mode Exit fullscreen mode

[country,state,city] dropdown list using javascript - Step by Step

Top comments (1)

Collapse
 
robertlook profile image
Robert Look