Building Dynamic Web Applications with AJAX

Joshua Eichorn

Creator of phpDocumentor, Senior Architect Uversa Inc.

What is AJAX

AJAX is an ancronym for Asynchronous Javascript And XML.

  • Ajax uses javascript combined with xml to grab information from a server without refreshing the page.
  • Its nothing new, the main requirement being that the web browser has an XMLHttpRequest object or equivalent
  • The term AJAX was coined Jesse James Garrett in February 2005
  • Some would say the coining of the new term has caused a Tipping point to be reached

So I can communicate with my server in a different way. Now what?

The two ways to look at AJAX

AJAX can be used to enhance your current web apps (generally optional)

AJAX can be used to provide a backend to a javascript application

The big difference is in the amount of javascript your writing and the feel of your application

Don't overuse ajax, the usability requirements for javascript applications are quite different than the requirements for regular web pages

Examples of AJAX apps

Using AJAX to have a big impact

Usability Tips

These are mainly taken from an article by Thomas Baekdal

Why a Toolkit?

When you don't want a toolkit

Example Application

Example Error

Example Success

JPSpan

Architecture

JPSpan server

// Include the needed classes require_once 'JPSpan/JPSpan.php'; require_once JPSPAN . 'Server/PostOffice.php'; // Create the PostOffice server $S = & new JPSpan_Server_PostOffice(); // Register your class with it... $S->addHandler(new username_validator()); if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'], 'client')==0) { $S->displayClient(); // Display the Javascript client } else { require_once JPSPAN . 'ErrorHandler.php'; // PHP errors serialized to JS // Start serving requests... $S->serve(); }

Validation Class

I'm only showing the method that does actual work

function check_username($username) { $ret = new stdClass(); $ret->status = true; if (in_array($username,$_SESSION['usernames'])) { $ret->status = false; // make the current username unique $i = 1; $new = $username.$i++; while(in_array($new,$_SESSION['usernames'])) { $new = $username.$i++; } $ret->message = "Username $username already in use,". " maybe you could use '$new' instead."; } return $ret; }

The link to the Server

The javascript proxy object is autogenerated from the php class in Server.php you could easily add a cache to increase scalability

<script type='text/javascript' src='server.php?client'></script> <script> // our remote proxy object var usernameValidator = new username_validator(new valCallback()); // our callback class function valCallback() { } valCallback.prototype.check_username = function(result) { if (!result.status) { setFormMessage(result.message); ... } }

Enforcing validation in onsubmit

function validateUsername(inSubmit) { var username = document.getElementById('username').value; if (inSubmit && usernameValidator.__client.xmlhttp.callInProgress()) { usernameValidator.__client.xmlhttp.abort(); usernameValidator.Sync(); var result = usernameValidator.check_username(username); if (!result.status) { setMesage(result.message); return false; } } else { usernameValidator.check_username(username); } }

Debugging Tips

Logging Code

To log JPSpan errors and success you need to write an observer class and setup the server to load it

class observer() { function error($data) { $fp = fopen('error.log','a'); fwrite($fp,var_export($data,true)); fclose($fp); } } // setup for logging define ('JPSPAN_MONITOR', TRUE); require_once JPSPAN . 'Monitor.php'; $monitor = & JPSpan_Monitor::instance(); $monitor->addObserver(new observer());

Resources

A closing remark

For a group of people who forget about hygiene for weeks on end developers sure have an obsession with cleanliness. First soap and now ajax, all we need is dawn and palmolive and you'll always have something to wash your hands and dishes.

If your reading this online feel free to leave questions/comments on my blog