phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


AJAX: Shortcut to AJAX

Tag: ajax   Category: PHP Basic
post: 27 Dec 2007 read: 2,338


AJAX Tutorial: In this post, We try to use Ajax in our simple application. First time, We will build simple ajax library. Then, we write page to load the ajax library. This is very basic application. You can still extend it for more complex web.

Ok, for this practice, we need 3 file (I create within www/test/ajax directory):

  1. ajax.js -> as ajax library
  2. test.php -> as main page
  3. home.html -> will be loaded as content

First, write below line codes within ajax.js


function callAJAX(url, pageElement, callMessage) {
      document.getElementById(pageElement).innerHTML = callMessage;
      try {
        req = new XMLHttpRequest(); /* e.g. Firefox */
      } catch(e) {
          try {
          req = new ActiveXObject("Msxml2.XMLHTTP");
   /* some versions IE */
          } catch (e) {
              try {
              req = new ActiveXObject("Microsoft.XMLHTTP");
  /* some versions IE */
              } catch (E) {
                 req = false;
              }
          }
      }

      req.onreadystatechange = function() {responseAJAX(pageElement);};
 	 req.open("GET",url,true);
      req.send(null);

  }

function responseAJAX(pageElement) {
    var output = '';
    if(req.readyState == 4) {
         if(req.status == 200) {
              output = req.responseText;
              document.getElementById(pageElement).innerHTML = output;
            }
       }
   }

then, we create test.php, enter following codes:


<html>
<head>
	<SCRIPT language="JavaScript" SRC="ajax.js"></SCRIPT>
</head>
<body onload="callAJAX('home.html','displaydiv')">

<table>
<tr>
<td id="displaydiv"></td>
</tr>
</table>

</body>
</html>
last, we create home.html, enter following line code:

front page test

Now, point your browser to http://localhost/test/ajax/test.php, you will get like this:

Next posting, we will discuss how to create load pages from links.



Series this article:
AJAX : Understanding AJAX
AJAX: Shortcut to AJAX
AJAX: Loading Web Content from Links
AJAX: Loading Preloader Animation Image

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting