4. Get the response
The response will be as string or as XML. The data sent back from the server can be retrieved with the responseText property as string. Use responseXML for getting the response as XML.
5. Use the response on your web page
You can use the response you got from the XMLHttpRequest in your web page/application. You can either set a value of a text field or use the returned HTML from the web request as innerHTML for a <div></div> tag or <span></span> tag (See below for the implementation of this)
Using AJAX with PHP
I usually place all the AJAX related functions in one JavaScript (ajax.js), and later add the JavaScript in my PHP pages. My ajax.js looks like below.
function createRequestObject(){var req;if(window.XMLHttpRequest){//For Firefox, Safari, Operareq = new XMLHttpRequest();}else if(window.ActiveXObject){//For IE 5+req = new ActiveXObject("Microsoft.XMLHTTP");}else{//Error for an old browseralert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera'); }return req;}
//Make the XMLHttpRequest Objectvar http = createRequestObject();
function sendRequest(method, url){if(method == 'get' || method == 'GET'){ http.open(method,url,true);http.onreadystatechange = handleResponse;http.send(null);}}
function handleResponse(){if(http.readyState == 4 && http.status == 200){var response = http.responseText;if(response){document.getElementById("ajax_res").innerHTML = response;}} }
Now I add a <script></script> tag in my PHP pages like following to access the JavaScript functions.
Then I create a function called sendReq() as shown above for preparing the URL to the PHP page to get some data from the database. If you look into the ajax.js, you will see that I’m assigning the output of the PHP page to a <div> tags innerHTML. My div tag has the id=’ajax_res’.
<div align="center" class="black" id="ajax_res"><img src="wt_bg.jpg"></div>
After calling the sendReq function with a value of ‘success’, it will connect to the server for sending a request to the URL get_lcm.php?status=success, then get the data from the database using the value ‘success’, and set the innerHTML attribute of the div tag whose id is ‘ajax_res’ according to the response. Initially my div tag shows an image, but after AJAX responds successfully this image will be replaced with the response. You can also get the response as the value of an input text.
My get_lcm.php looks like this.
<?phpinclude_once("create_table.php");
if(!empty($status)){echo "<strong>Search Result for Status: ".$status."</strong><br><br>";
$ct = new createtable("select * from lcm where state=$status");
$ct->table_viewer();}?>
I used one of my PHP classes ‘create_table.php’ for creating a table from SQL queries. ‘create_table.php’ has a function, table_viewer(), to print the SQL output as a HTML table. I will discuss about this class and some other data abstraction classes I used in some later articles, if necessary.
I had the following HTML code for calling the AJAX function using the sendReq() as described earlier.
<a class="none" href="#" onClick="sendReq(1)">ACTIVE</a>
If a user clicks the above hyperlink, then the JavaScript function sendReq is invoked using the value of status equal to 1. After that the webpage with URL get_lcm.php?status=1 will be invoked. This PHP will get the information from database and shows the result as table. This result will then be displayed within the <div> tag.
Using AJAX from PHP is very easy as described here. All you need is the JavaScript functions for sending the XMLHttpRequest and then handle the Http Response. It will simplify development is you place all your AJAX related code in a single JavaScript file and reference it from anywhere you need AJAX.
There is another way of getting the same result from PHP without using the XMLHttpRequest object. But I personally do not like the concept, because it is missing one of the main ingredients of Asynchronous JavaScript And XML (AJAX), XML. This concept is good for the old browsers with no supports for XMLHttpRequest object, but as all the newer versions are supporting XMLHttpRequest object you can use AJAX for the common browsers.