Creating a dynamic country state and city selector dropdown in web development is important to enhance the user experience, especially for forms that require location details. This guide provides a complete solution using PHP, jQuery, and MySQL, allowing users to select each dropdown based on their preferences. We’ll start by setting up our database structure, building the HTML form, and adding the necessary JavaScript to enable dynamic state and city populations based on the user’s country and state selection.
Create Database for country state and city selector
To begin, let’s create three tables in our MySQL database:
countries
: Stores country names.states
: Stores states with a foreign key referencing thecountries
table.cities
: Stores cities with a foreign key referencing thestates
table.
Here’s a SQL script to create and populate the database:
CREATE DATABASE countrystatecity;
USE countrystatecity;
-- Table for countries
CREATE TABLE `countries` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `countries` (`name`) VALUES ('India'), ('Nepal');
-- Table for states
CREATE TABLE `states` (
`id` INT NOT NULL AUTO_INCREMENT,
`country_id` INT NOT NULL,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`country_id`) REFERENCES `countries`(`id`)
);
INSERT INTO `states` (`country_id`, `name`) VALUES
(1, 'Delhi'),
(1, 'Uttar Pradesh'),
(2, 'Madhesh'),
(2, 'Bagmati');
-- Table for cities
CREATE TABLE `cities` (
`id` INT NOT NULL AUTO_INCREMENT,
`state_id` INT NOT NULL,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`state_id`) REFERENCES `states`(`id`)
);
INSERT INTO `cities` (`state_id`, `name`) VALUES
(1, 'New Delhi'),
(2, 'Lucknow'),
(3, 'Birgunj'),
(4, 'Kathmandu');
Create database connection for country state and city selector in php
Create a conn.php
file to handle the database connection:
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Creating Country, State and City Dropdown Form
The HTML form will have three dropdowns. When a country is selected, it triggers an AJAX call to fetch the corresponding states. Similarly, selecting a state triggers an AJAX call to fetch the corresponding cities.
Country, State, City Dropdown
Backend logic for state and city retrieval
Create two PHP files—getStates.php
and getCities.php
—to retrieve states and cities respectively based on the selected country or state.
getStates.php
query("SELECT * FROM states WHERE country_id = $country_id");
echo '';
while ($row = $result->fetch_assoc()) {
echo "";
}
}
?>
getCities.php
query("SELECT * FROM cities WHERE state_id = $state_id");
echo '';
while ($row = $result->fetch_assoc()) {
echo "";
}
}
?>
Conclusion
Their tutorial provides a step-by-step guide to creating a dynamic country-state-city dropdown using PHP, jQuery, and MySQL. By implementing this feature, you increase the usability and interactivity of your web forms, making it easier for users to select relevant geographic data.
Select country, state and city dropdown source code
Send download link to: