DEV Community

Cover image for PHP MySQL Country State City Dropdown using Ajax Example Tutorial
Sonagrabhavesh
Sonagrabhavesh

Posted on

PHP MySQL Country State City Dropdown using Ajax Example Tutorial

Today, I will learn you how to engender php mysql country state city dropdown utilizing ajax.dynamic country state city database utilizing ajax in mysql php. in this tutorial, we will show you how to dynamically populate country state city dropdown list in php utilizing jquery ajax from mysql database.

We will guide you step by step on how to populate country state city in dropdown list onchange in PHP from MySQL utilizing Ajax or populate the first, second, and third dropdown predicated on the first, and second cull of dropdown in PHP. As well as learn, how to fetch data from the database in the dropdown list in PHP utilizing jQuery ajax.

I will dependent country state city dropdown utilizing ajax in PHP MySQL, we will show states and cities in the dropdown list predicated on culled country and verbally express value in PHP utilizing ajax from the database.

Country State City Dropdown List in PHP MySQL using Ajax

Now, follow below given simple and facile steps to retrieve and exhibit country, state, and city dropdown list onchange in PHP utilizing jQuery ajax from MySQL database:

  • 1Create Country State City Table
  • 2Insert Data Into Country State City Table
  • 3Create DB Connection PHP File
  • 4Create Html Form For Display Country, State and City Dropdown
  • 5Get States by Selected Country from MySQL Database in Dropdown List using PHP script
  • 6Get Cities by Selected State from MySQL Database in DropDown List using PHP script

Step 1: Create Country State City Table

In this step,open your database and run the following sql queries to engender country, state and city table into the database.

CREATE TABLE `countries` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
 `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Next, Run this following sql query to engender state table into your database:

CREATE TABLE `states` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `country_id` int(11) NOT NULL,
 `name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
 `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active | 0=Inactive',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Here, Run this following sql query to engender city table into your database.

if you want to see full example follow bellow link..

https://codingtracker.blogspot.com/2021/05/php-mysql-country-state-city-dropdown.html

Top comments (0)