3 Practical Uses For AJAX

AJAX — or Asynchronous JavaScript and XML is the newest buzz word in web development. Utilizing a client-side XMLHTTPRequest object, HTML, JavaScript and CSS, you can replace the page refresh that has (up until a few months ago) been a mandatory part of posting data back to a web server.

In this article I’m going to show you 3 simple and practical uses for AJAX on your web site. You’ll need to know a bit of HTML, JavaScript and PHP to follow along, but I’ll do my best to explain everything in simple terms so you don’t get lost!

Note: In this article I will assume that you’re running a PHP web server.

Enabling Client-to-Server Communication
 
There are 2 simple functions that are required for AJAX to work. They handle everything needed to pass data to the server and also to process the response back from the server. We will create a .JS file containing these 2 functions. They will be used in each of our 3 AJAX examples.

Create a new file in your favorite text editor and call it ajax.js — copy and paste the following code into the file and save it:
 
function DoCallback(data)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open(‘POST’, url, true);
        req.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
        req.send(data);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject(‘Microsoft.XMLHTTP’)
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open(‘POST’, url, true);
            req.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
            req.send(data);
        }
    }
}

function processReqChange() {
    // only if req shows ‘loaded’
    if (req.readyState == 4) {
        // only if ‘OK’
        if (req.status == 200) {
            eval(what);
        } else {
            alert(‘There was a problem retrieving the XML data: ‘ +
                req.responseText);
        }
    }
}

Now that you’ve created your ajax.js file, let’s move onto our first use for AJAX on your web site.
Instead of posting back to the server to validate a username/password for a login form, let’s see how you can do it using AJAX.

Let’s start by creating the PHP script that AJAX will

This entry was posted in Featured, Programming, Web Design, Web Technologies. Bookmark the permalink.

13 Responses to “3 Practical Uses For AJAX”

  1. Azizur Rahman says:

    I have already implemented the Login script in ajaxa last week. I have my login script to work with a LDAP Server. this has to be best feature that has been added to our office intranet given so many issue proxy servers and so on. on top of that it also lot more user friendly.

  2. Aaron says:

    Great job Mitchell, your the man…

  3. saleem says:

    A very good script espeially for php programmers.
    Thanx a lot.

  4. Wilson Delgado says:

    Best tutorial, very clear and easy. Thanks for you work (For Spanish Users: Muy buen tutorial, bastante facil de entender y realmente funciona al implementarlo)

  5. guangming hu says:

    good example good code!

  6. TET says:

    THIS IS A GREAT FEATURE

  7. Scumop says:

    Very nice explanation and examples. I had no idea it could be so easy.

  8. Nanda says:

    Good Tutorial… Thanks.

  9. scott says:

    what does eval(what) do ?

  10. Javier says:

    Hey, this tutorial rocks!
    i've been searching for houres, and this is the best ajax tutorial with example! congrats dude!

  11. Steve says:

    Very good tutorial, thank you!

  12. Karthik says:

    Really good work, thanks.

  13. paul bogle says:

    great tutorial, helped me a lot. thanks.

Leave a Reply