Use of AJAX with PHP

Use of AJAX with PHP

Use of AJAX with PHP

jQuery Ajax is a powerful jQuery tool that may be used to build a visually appealing webpage. The Ajax capacities in jQuery return an article that is a superset of the XMLHttpRequest object.

GET and POST are the two most frequent ways for a request between a client and a server.

GET:- Requests data from a particular resource, and is mostly used to retrieve data from the server.

POST:- Sends data to a specific resource to be processed; it's used to send data to the server and also to retrieve it. Data is never cached when using the POST method.

Ajax requests are similar to web page calls. The only difference is that the user will not have to reload the page and will be able to execute user actions. You must first include a JQuery link in your page before making an Ajax request.

Let’s understand AJAX with following example:-

1. Create Database Tables

Three tables in the database are required to contain the data for the country, state, and city. There would also be a connection between the countries, states, and cities tables. The states table and the nations table are linked, and the cities table is linked to the states table.

2. Database Configuration (dbConfig.php):-

The PHP and MySQL code below is used to connect to the database.Based on your database credentials, specify the database host ($dbHost), username ($dbUsername), password ($ dbPassword), and name ($dbName).

// Create database connection :-
// Check connection:-

HTML & PHP Code:-

When a value in the nation dropdown is chosen, the data for that state and city is displayed in the state and city dropdowns.

JavaScript Code:-

When a country is chosen, the reliant states are obtained from the server side content (ajaxData.php) utilizing jQuery and Ajax. Similarly, by choosing the express, the reliant urban areas are recovered from the worker side content (ajaxData.php) utilizing jQuery and Ajax.

To use Ajax you need to use the following cdn.

Run an Ajax inquiry to recover states and cities from the data set without reloading the whole page.

A. On changing the value of country dropdown,

1. POST the chosen country ID to the server side content (ajaxData.php).

2. The data is recovered from the successful run of AJAX and HTML is set to the state dropdown.

B. On changing the value of state dropdown,

1. Present chosen state ID will be sent to the server side(ajaxData.php) through Ajax command.

2. The data is recovered from Ajax and HTML is set to the city dropdown.

Retrieved Dependent Data from Database (ajaxData.php):-

The ajaxData.php is called by the Ajax solicitation to recover the reliant information from the data set utilizing PHP and MySQL. The state and city dropdown HTML is returned to the achievement technique for the Ajax command.

Recover the ID from the Ajax command using PHP $_POST technique.

A. In case country_id is given-

1. Get the state information dependent on the particular country ID from the data set.

2. Update HTML of the states list.

B. If state_id is provided-

1. Get the city information dependent on the particular state ID from the data set.

2. Update HTML of the cities list.

Different options of the dropdown list:-
Generate HTML of city options :-

Publish and get value from dynamic dropdown

After the above operation, you can get the value of the particular select boxes using PHP. Use the $_POST method to recover the required value in PHP.

HTML Code:

PHP Code:


The unique select boxes are an exceptionally helpful component when you need to permit the client to choose values from the dropdown list. In the model code, we displayed a selection box for the State City drop-down menu in PHP. Select boxes useful and carry out Ajax country state city dropdown with PHP and MySQL. On the off chance that you have information, use the JSON type in Ajax and PHP for a quick reaction.

I Hope you enjoy this blog

Thank You

Want a Team that Delivers Result ? Connect now with us.


Our Offices


F-429, Phase 8B, Industrial Area, SAS Nagar, Punjab 160059

+91 82198-18163


13506 Summerport Village Pky Suite 355 Windermere, FL 34786

+1 (321) 900-0079


15 Meltwater Cres, Brampton L6P3V8

+1 (647) 892-6147