AJAX, AJAX Agent for PHP


You have undoubtedly heard the buzz about AJAX. I started reading up on this months and months ago; however if you do any kind of development - it's hard for you to get a handle until you actually DO something practical with a new technology. I'm working on a new web site, and I've implemented just a "bit of Ajax" to enhance usability.

I'm someone who likes to maybe browse through a book if I'm learning something new (most often some aspect of operating systems or development). Early on, I was disappointed by the books offering information on Ajax, and the websites with information were disappointing also. Read on for some down and dirty information for getting Ajax, or Ajax-like usability enhancements on your web site.

First of all, AJAX stands for Asynchronous JavaScript And XML. However, you aren't forced to use XML. Actually XMLHttpRequest is a JavaScript instruction (implemented slightly differently in different browsers, but this is a negligible isse). This instruction essentially tells your web page to "fetch data" behind the scenes. It's usually augmented with some form of Dynamic HTML (XHTML/CSS) for interactivity.

Here's a simple example of AJAX in action: Let's say you are at a web site, and you have a simple form for your users to provide a username and password to log in. Traditionally, users type their name - then their password and click a submit button. If the user credentials are wrong, a new page is then returned to the browser. With AJAX, as soon as the user is done providing his or her username, you can be checking the validity of the provided username with a database query that is happening behind the scenes. Your user is almost instantly notified if they have provided bad information. Trust me, your users will enjoy less page loads - even on fast connections.

This concept can be twisted in a number of brilliant concepts... many of which lead to dynamically updating your page whenever a user clicks the submit button - or perhaps without even having a submit button involved at all. If you have a complex database, you can update your search results on the same page - as the user changes his criteria. Google uses the concept very often - most notably for Google Maps.

Writing AJAX code tailored to your scripting language can kind of be a hassle; fortunately there are a number of AJAX "Frameworks" available for this need. My favorite, as I use PHP often, is AJAX Agent. There are some good examples at their web site. For AJAX Agent, you have a PHP file - that includes a reference to the AJAX Agent library. You'll then have two JavaScript functions - one that is called by your web page or form with an event - and one that is "called back" when your data is available. This can be a simple JavaScript alert box or a slick CSS component that appears on the page. That's it.

I have a hunch that AJAX isn't catching on as fast as it should, because it's being touted as it's own technology. It's cool, but it should really only be considered a facet of JavaScript as a whole. If you can figure out JavaScript and dynamic web pages - you can work with AJAX. If you're a web developer, Google for "AJAX Framework" along with your scripting language of choice. You'll be amazed at how easy it is to implement.