I'll show you how to make a dynamic drop-down list in PHP using MySQL and Ajax today.
You saw a lot of online e-commerce applications, such as dropdown user interfaces for states and cities. The state list is automatically generated and the city data is filled in with the previously selected state and country information once users select a country from a drop-down list.
In this article, I used three dependent dropdowns Country, State, and City that dynamically load data from MySQL into drop-down options without requiring a page refresh. I also used PHP to implement jQuery and Ajax scripts.
Create a Database and Tables
Within PHPMyAdmin. I made a database called "drop_down_list_php_db" and three tables named "country," "state," and "city" inside of it.
The state table and the country table have links, and the city table and the state table have links as well.
Table: Country
In this table, I added two columns country_id and country_name.
I have inserted two records in the country table.
Table: State
I have three columns in this table: state_id, state_name, and country_id. The country_id column will function as a foreign key and will match the id in the country database to make the state select box dependent on the country's select box.
I have inserted four data into the state table.
Table: City
Three columns make up this table: city_id, city_name, and state_id. state_id will function as a foreign key for this table, just like it does for the state table.
I've added eight data to the city table.
Here, I have provided the ERD diagram to see the foreign key relationship between the country, state, and city tables.
Check out this example to see how a dynamic drop-down list works.
Project File Structure
I have created four PHP files that incorporate jQuery and Ajax to create a dynamic drop-down selection option.
dbconfig.php
This file contains PHP code that creates the connection to a MySQL database using the PDO extension.
index.php
In this file, I have put three select boxes and I retrieve country records from the country table in the country select box.
Implement jQuery Ajax Code
I have retrieved state and city data from the database using Ajax without having to restart the page.
I have added the code below to the index.php file. When the dropdown option value is selected, it sends the country_id and state_id to the server-side scripts state.php and city.php via an Ajax call. We then receive the response and show the HTML data in the corresponding dropdown list.
state.php
This file contains PHP code that, upon selection of the country box, loads data dynamically and pulls state data from the state table.
city.php
When a selection is made on the state box, this file's PHP code loads data dynamically and selects city records from the city table.
Learn More:
What does array splice do in PHP
What is POST variable in PHP
What is Custom PHP Development
How do you append to text in PHP
How to make OTP Verification in PHP
How to store textarea value in database in PHP
How to Fetch Data from Database in JSON format in PHP
How to get the class name in PHP
How to Run PHP Code in Browser
How to Check if a String is Null in PHP
How to Pass Dropdown Selected Value to Database in PHP
How to Process a Form using PHP
Download Codes
No comments:
Post a Comment