PHP and AJAX
AJAX
refers to updating the webpage component without the entire page being reloaded.
What is AJAX
AJAX = Asynchronous JavaScript and XML.
AJAX is a rapid and dynamic webpage creation tool.
AJAX permits the asynchronous actualization of web pages through modest quantities of data to be exchanged with the server behind the scenes. This means that elements of a web page can be upgraded without the complete page reloading.
If the content should change, classic web pages (which do not use AJAX) need to reload the complete page.
Google Maps, Gmail, Youtube, and Facebook tabs are examples of AJAX apps.
How AJAX Works
Related Links
AJAX is Based on Internet Standards
AJAX employs a mixture of The Internet standards :
- XMLHttpRequest object (to exchange data asynchronously with a server)
- JavaScript/DOM (to display/interact with the information)
- CSS (to style the data)
- XML (often used as the format for transferring data)
Browser and platform independence are key features of AJAX apps.
Related Links
Steps To Create An AJAX Program
There are few steps available to create an ajax program which is:
- Create an object for
XMLHttpRequest
class. - Implement
onreadystatechange
event to check the status of request. - Open a request connection with specified server page.
- Send request to server.
Create XMLHttpRequest Object
It is used to exchange data asynchronously from a server page.
The following syntax is used to create object is:
var xmlhttp = new XMLHttpRequest();
Implement onreadystatechange Event
It is used to track the status of our request with a server page. We can show different types of output depends on the request status.
The following syntax is used to implement onreadystatechange event is :
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
// Write your output code if request successful
}
Open Connection
It is used to exchange data asynchronously from a server page.
The following syntax is used to create object is:
xmlhttp.open("GET", "serverpage", true);
Send Request
After opening a connection, you can send the request to server.
The following syntax is used to send request is :
xmlhttp.send();
PHP and AJAX Example - Getting Server Date and Time
First we will create a html file called "showtime.html" and it contains a button to get and display the server date and time.
Example:- showtime.html
The server time is :
Second we will create a php file called "showtime.php" and it contains the code to display the server date and time.
Example:- showtime.php
Output:-
The server time is :
PHP and AJAX Example - Getting Hint Text Like Google Search Suggestions
This example shows how a Web page can communicate with a web server when a user type in a field.
First we will create a html file called "showhint.html" and it contains a button to get and display the server date and time.
Example:- showhint.html
Start typing a country name in the input field below:
The suggestions are
Second we will create a php file called "gethint.php" and it contains the code to display the hint text.
Example:- gethint.php
Output:-
Start typing a country name in the input field below:
The suggestions are
In the above example, a function called "showHint()"
is used if a user types a character in the input field.
The function is called when the onkeyup
event occurs.
Code explanation :-
To begin, make sure the input field is empty (str.length == 0
). If that's the case, clear the hint1 placeholder's content and leave the function.
However, if the input field is not empty, do the following :
- Generate an
XMLHttpRequest
object. - Generate a function that will be called once the server response is complete.
- Send the application on the server to the PHP file (gethint.php).
- The
q
argument has been added to the url (gethint.php?q="+str) as you can see. - The content of the input field is stored in the
str
variable.