irwan

Simple AJAX - PHP and Javascript

Apr 22nd, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.30 KB | None | 0 0
  1. This tutorial will cover just the basics with AJAX, and we will start with the base object you need: a XMLHTTP Javascript object. With this object you call a server-side script and get the response back. Most browsers use the same Javascript object, but of course Microsoft has to be different, so they require their own. The following function will return the correct object:
  2.  
  3. function getXMLHttp()
  4. {
  5.   var xmlHttp
  6.  
  7.   try
  8.   {
  9.     //Firefox, Opera 8.0+, Safari
  10.     xmlHttp = new XMLHttpRequest();
  11.   }
  12.   catch(e)
  13.   {
  14.     //Internet Explorer
  15.     try
  16.     {
  17.       xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  18.     }
  19.     catch(e)
  20.     {
  21.       try
  22.       {
  23.         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  24.       }
  25.       catch(e)
  26.       {
  27.         alert("Your browser does not support AJAX!")
  28.         return false;
  29.       }
  30.     }
  31.   }
  32.   return xmlHttp;
  33. }
  34.  
  35. This code will, again, return an XMLHTTP object to use in our Javascript. What is going to happen is that we are going to use this object to make an HTTP request through Javascript. When we get a response back, we will simply place it in the page, nothing fancy. In order to make the request we use the following code:
  36.  
  37. function MakeRequest()
  38. {
  39.   var xmlHttp = getXMLHttp();
  40.  
  41.   xmlHttp.onreadystatechange = function()
  42.   {
  43.     if(xmlHttp.readyState == 4)
  44.     {
  45.       HandleResponse(xmlHttp.responseText);
  46.     }
  47.   }
  48.  
  49.   xmlHttp.open("GET", "ajax.php", true);
  50.   xmlHttp.send(null);
  51. }
  52.  
  53. The key to this whole process is using the XMLHTTP member readyState, which is pretty much exactly what it sounds like. The readyState corresponds to the state of the object itself, which can be anything from Uninitialized to Loaded. In the case of an AJAX request, we wait for the readyState to be 4, which is the maximum value and tells us that all the data to be loaded from the request is. So when the readyState is 4, the request is done and the data is ready for display.
  54.  
  55. Once you take care of catching the readyState, you need to make the actual request, using the open() and send() methods. The open method is what we use to set up the request. It takes in a request type (ie "post" or "get"), the page URL, and a boolean value indicating wheather we are making an asynchronous call or not. In this case we are, so it is true. So to set up the open() call, we use GET, ajax.php for the url, and set the asynchronous boolean to true.
  56.  
  57. After you have your open() method all set up, you need to call send() to make the actual request. The send() method makes the request, sending all the arguments you give it. For this case, we are sending nothing, so the arguments are null. When we get the response back, we have to do something with the data sent back. We use the HandleResponse() function to display the information sent back, when the readyState is 4 of course. The HandleResponse() function look like so:
  58.  
  59. function HandleResponse(response)
  60. {
  61.   document.getElementById('ResponseDiv').innerHTML = response;
  62. }
  63.  
  64. All this function does is take the response from the request and puts it in a div on our page. Now, that is all our Javascript, which works just fine all in one file, which we will call ajax.js. Now that we have our Javascript, we need to make our ajax.php file, with is one monsterous file, full of complex code:
  65.  
  66. <?php
  67. echo "This is a php response to your request!!!!!!";
  68. ?>
  69.  
  70. Yep, that is it. One echo statement is all we really need for this tutorial. The concept is that all we need to do is send something back to the XMLHTTP object. In the simplest form, this is just some text, where PHP's echo statement works perfectly. This will return our statement back and Javascript will display it on the page. But what about the page eh?
  71.  
  72. The page in question can really be anything as long as it calls the MakeRequest() method in our javascript file. In our case we will make a simple index.html page that will have a button and a div. The code will look something like:
  73.  
  74. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  75. <html>
  76.   <head>
  77.     <script type='text/javascript' src='ajax.js'></script>
  78.     <title>PHP AJAX Example</title>
  79.   </head>
  80.   <body>
  81.     <input type='button' onclick='MakeRequest();' value='Use AJAX!!!!'/>
  82.     <div id='ResponseDiv'>
  83.       This is a div to hold the response.
  84.     </div>
  85.   </body>
  86. </html>
Advertisement
Add Comment
Please, Sign In to add comment