Country State and City Dependent Dropdown in PHP & MYSQL

Are you looking for dependent country state and city dropdown for your application. In this post we have exlained how to create dropdown dependent select list using AJAX and MYSQL.

You have found country-state-city dropdown UI interface in many ecommerce or course based web applications. Where user first choose country from dropdown list, then state list automaticlly produced and then cities data populate on the previous selected country and state data.

I have seen many tutorial also published on web, but they are showing dependent country state and city dropdown by Id value. I mean when your submit the form, you will get country, state and city IDs, instead of country code or state code.

dependent dropdown list

In this post, we have explained the process to create Ajax dynamic dependent country state city dropdown using PHP and MYSQL. In which you will get Country ISO Code, State ISO Code and City name on form post submit.

What’s Inside:

  1. Create Database Tables
  2. Make Database connection
  3. Create Dropdown Elements in HTML Form
  4. Write jQuery Ajax Code to Get Element on Change Event
  5. PHP code to fetch Dependent dropdown data
  6. Get Selected data on Form Submit

Steps to Create Dependent Country State City Dropdown

1) Create Database Table:

a) Countries Database Table Structure: First create ‘countries’ database table using below MySql command. This table includes the iso3, iso2, phonecode, capital, currency and currency symbols.

b) Create State Database Table: use below SQL command to create ‘state’ database table structure.

c) City database table structure:

2) Make Database connection:

Now create a database.php file to make MySQLi connection.

3) Create Dropdown Elements in HTML Form:

Now we need to create dropdown select HTML elements for country, state and cities data.

Create index.php file and include ‘database.php’ in it.

First, fetch all countries database data from database which have flag = ‘1’, means all active ones. Show all countries data in first country dropdown list.

Index.php

4) Write jQuery Code to Get Dropdown On Change Event:

Now create a ‘custom.js’ file, in which write jQuery code to perform action on select on change event.

In this we have called Ajax Post method to show data in dropdown lists.

Note: Must include this custom.js file in Index.php script file.

custom.js

5) Write Code to Fetch dependent dropdown data:

Copy below ‘getAjaxData.php’ script code to show country state and city dropdown data.

getAjaxData.php

6) Get Selected data on Form Submit:

On form submit, you can get each selected dropdown list data via $_POST. This will return country code, state code and city name value.

Conclusion:

By following above steps, you can easily implement dependent dropdown UI interface feature in your web application. To get all countries, state and cities MySQL data, get our complete code package from belwo given button link.

Download   |   DEMO

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish