Ajax & PHP scripts

PHP is a server-side scripting language, while Ajax (Asynchronous JavaScript and XML) is a client-side technique for making asynchronous requests to the server. When using PHP with Ajax, we typically interact with the server to retrieve or send data without requiring a full page reload. Here are the main sections involved in using PHP with Ajax:
  1. Client-Side (Frontend):
    1. HTML: We create the structure of our web page where we'll place the elements that interact with Ajax.
    2. JavaScript: We write the client-side code that uses Ajax to make requests to the server and update the page without reloading it. We can use the `XMLHttpRequest` object, the newer `fetch` API, or even libraries like jQuery or Axios to simplify the process.
  2. Server-Side (Backend):
    1. PHP Script: This is where we process the Ajax requests on the server side using PHP. Depending on the type of request (GET or POST), we can access data sent by the client, perform actions, query a database, or generate responses.
  3. Communication:
    1. Ajax Requests: In our client-side JavaScript code, we initiate Ajax requests using methods like `XMLHttpRequest` or `fetch`. We configure these requests to target a specific PHP script on the server.
    2. Server-Side Processing: The PHP script on the server receives the Ajax request, processes it, and generates a response.
    3. Response: The server sends the response back to us. This could be in various formats, such as JSON, XML, HTML, or plain text, depending on what our application needs.
  4. Handling Responses:
    1. Client-Side Response Handling: In our client-side JavaScript code, we handle the response received from the server. This might involve updating parts of the web page, displaying messages, or performing other actions based on the data returned from the PHP script.
  5. Error Handling:
    1. Client-Side Error Handling: We need to handle potential errors that might occur during the Ajax request, such as network issues or server errors. This ensures that our application provides a smooth user experience even in case of failures.
    2. Server-Side Error Handling: Within our PHP script, we should include error handling mechanisms to catch and handle any issues that might arise during processing.
  6. Security Considerations:
    1. Cross-Site Scripting (XSS) Protection: We sanitize and validate data coming from the client side to prevent malicious code injection.
    2. CSRF Protection: We implement measures to prevent Cross-Site Request Forgery attacks by including and verifying tokens in our requests.
  7. Integration with Databases:
    1. If our application requires data retrieval or manipulation, we might interact with a database using PHP. This involves using PHP's database connectivity features (e.g., MySQLi or PDO) within the PHP script that handles the Ajax request.

Connecting to a JavaScript file

We can keep our JavaScript file used for Ajax inside the main file or can keep in external .js file. Any external JavaScript file can be included inside the main file like this.
<script src='query.js'></script>

Checking Script support

As Ajax depends on client side scripting, we need to check the script support is enabled ( in browser ) or not. Here is the code we can use to display message if scripting is disabled.
<noscript>Your browser does not support scripts, you have to enable it to run this page</noscript>
Let us start with some examples on uses of Ajax

XMLHttp object

To work in Ajax we have to create XMLHttp request objects based on type of browser. This object we will use to pass data and collect the data from the server.

Form submission in GET method

Using GET method we can submit form details to a backend PHP script using Ajax

Server Clock

Displays a clock showing server time where request is send in every second to server to get the data

Adding options to a datalist

Datalist can be used for auto complete of a textbox. We will learn how to add options to a datalist.

dependent list box using Ajax

We will be using Ajax to send the selection value of first drop down list and then based on this value we will send one more dependent drop box.Read More

Validation of email address

We will use Ajax to pass email data as entered by user and then this address is checked with a validation code written in PHP. Read More

Customer Details from Server

Using Ajax we can Post customer id to server and collect the matching details of the customer. . Read More

PHP MySQL Paging script using AJAX

We can create a paging script by breaking records into pages using Ajax, PHP and MySQL database. . Read More

Message Display by using Ajax & PHP

We need to communicate with visitors while processing data in server. Learn the basics of displaying, hiding messages using layers by DIV tag.

Ajax based progress bar

The status of script exection can be shown by a growing progress bar. This is the basic of how to display a growing progress bar.

MySQL PHP Ajax based progress bar

Data is taken from a MySQL table and status is updated from the main script.

Form validation using Ajax PHP

We can display the error messages without reloading the page or sending all the data to server side script by using PHP and Ajax

Form validation by using onBlur event with Ajax and PHP

While filling the data we can validated the form by sending the details to server side script by using Ajax and PHP.


What is Json ? How to know Json support is available in our PHP or not? What we can do with our Json functions? . . Read More

Keyword Search in MySQL table as we type

We can get the result from our database table by applying search command. Here data is posted to backend query as we type and results are displayed.

Records based on selection of drop down list box

Records of a sub-category can be displayed after selection of a category from a drop down list box.
Demo Link Description
ListBoxOn select of List box details of that record is displayed
Linked RecordsOn Selection of one listbox option all the records linked to it will be displayed
Paging of recordsWith Backword and Forward buttons and displaying 10 records per page
Keyword SearchDisplay the matching search result as we type the chars
Form ValidationValidating all inputs entered by user in a web form
Form Validation IIValidating inputs as soon as focus is shifted out in a form
Signup & login methodValidation of Signup form and displaying error messages without reloading

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net






    post comment


    testing the form


    testing the data.


    Ajax combined with PHP is so interesting, I thought programming with ajax is difiicult but now my mind is opened. So if I can ask, Is there a specific part of which when I am saying I want to learn Ajax, I should start with?

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer