AJAX stands for Asynchronous JavaScript And XML. Any server side technology that supports JavaScript also supports AJAX. AJAX is a browser technology, and is therefore independent of web server platforms.
In this article we will learn about what AJAX is, how it works, and how can we use AJAX with PHP. Please remember, AJAX is not a programming language, so you don’t have to learn any new technology. AJAX can be implemented by using existing standards (JavaScript and XML) in a different way. If we are using PHP or any server side technology and need to extract data from storage on a server (eg a database or a file), we will have to make an HTTP request (either POST or GET) to get the data. Once the data is received the the web page will need to be reloaded to show the data. Using AJAX technology we can request and receive the data from server in background and then display it on the page without a reload. AJAX uses HTTP requests for this. With AJAX, JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object (XML over HTTP). With an HTTP request, a web page can make a request to, and get a response from a web server without reloading the page. The XMLHttpRequest object is supported in Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7. But the creation of XMLHttpRequest object is different in Internet Explorer than the other browsers. I will discuss this later. To use AJAX to request a data from the server we need to do the following.
1. Create an XMLHttpRequest object. 2. Then using this object, request data from the server. 3. JavaScript will then monitor for the changing of state of the request.4. If the response is successful, then the content from the data store requested will be returned as response (response can be in the form of a String or XML).5. Use the response in your web page.
1. Create an XMLHttpRequest object
JavaScript has a built-in XMLHttpRequest object. You can use that for Firefox, Safari, and Opera. For Internet Explorer use the ActiveXObject, there is also a difference between IE 5.0 and IE 6.0+ in how to create the object. The following codes creates an XMLHttpRequest for all browsers:
var req;
if(window.XMLHttpRequest){//For Firefox, Safari, Operareq = new XMLHttpRequest();}else if(window.ActiveXObject){//For IE 5req = new ActiveXObject("Microsoft.XMLHTTP");} else if(window.ActiveXObject){//For IE 6+req = new ActiveXObject("Msxml2.XMLHTTP");}else{//Error for an old browseralert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');}
Here, first we are using the built-in JavaScript function XMLHttpRequest() for creating an XMLHttpRequest for Firefox, Safari and Opera. If the browser does support window.ActiveXObject, then it is Internet Explorer. For IE versions 5.0+, use new ActiveXObject("Microsoft.XMLHTTP") and for IE 6.0+ use new ActiveXObject("Msxml2.XMLHTTP"). If the browser does not support the built-in JavaScript function XMLHttpRequest() or ActiveXObject, then it does not support AJAX. You can also use JavaScript try-catch blocks for the same output. var req;try{// Firefox, Opera, SafarixmlHttp=new XMLHttpRequest();}catch (e){// Internet Explorertry{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch (e){alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera'); }}}
In JavaScript, if statements within a try section fail, then the execution resumes from the corresponding catch block. Here first we are trying to get create a XMLHttpRequest using the built-in function, and if it fails then we will try using ActiveXObject("Msxml2.XMLHTTP"), and if it fails also we will try ActiveXObject("Microsoft.XMLHTTP"). If all these fail, then we will alert the user that his/her browser does not support AJAX.
2. Request for a web page
After creating the XMLHttpRequest we now need to send the web request using the open method. We also need to specify the HttpRequest method, GET or POST. Use the following code to send the request.
req.open(“GET”,”somedata.php”);req.send(null);
Here, req is the XMLHttpRequest object. It will request to the server for somedata.php using GET method. The open function also has a third parameter, an optional boolean parameter. You should set that to true :req.open(“GET”,”somedata.php”,true);req.send(null);
Both of the above is correct.
3. Monitor for the response of the request
You will need to monitor for the state of the request. For doing this you can assign a function to req.onreadystatechange (Here, req is the XMLHttpRequest object), like below.
req.onreadystatechange=function(){if(req.readyState==4 && req.status == 200){var resp = req.responseText;}}
Or like this,
req.onreadystatechange = handleResponse;
function handleResponse(){if(req.readyState == 4 && req.status == 200){//returned text from the PHP scriptvar response = req.responseText;} }The readyState property holds the status of the server's response. Each time the readyState changes, the onreadystatechange function will be executed. Here are the possible values for the readyState property:State Description0 The request is not initialized1 The request has been set up2 The request has been sent3 The request is in process4 The request is complete
And status is the status of the HTTP Request, like 500 Internal Server Error, 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found etc. 200 means no error.