Thursday, March 23rd, 2006

Writing Your Server-Side Ajax Handler

Category: Ajax, PHP

Following up on his previous article on Ajax templating, Matthew Batchelder has posted the other half of the equation – the PHP script that responds to the Ajax requests.

In my recent post on ‘Ajax; Templating; and the Separation of Layout and Logic,’ I refer to an Ajax Handler that sits server side to handle Ajax calls. Some elaboration is in order.

As I have stated in the past, I’m a huge fan of Prototype AND I choose PHP as my language of choice…so my examples will be using both, but the idea is portable.

The code he provides illustrates the steps of setting up the base functions and creating a front controller-type interface to accept the Ajax calls. The basic examples simply echo back strings, but you get the idea. There are actually three different methods he shows to handle the incoming requests – using PHP’s eval function, doing the same process with variable variables, and handling the various types with the switch functionality.

Each handler looks to the GET string (the URL) for its data, pulling in the information and executing one of the predefined functions (“world” or “sweetWorldAction”) based on what the value of “function” is. It’s a pretty simple setup, but it’s easy to see how it could be extended to higher-level, more robust applications.

Posted by Chris Cornutt at 7:11 pm

3.6 rating from 66 votes


Comments feed TrackBack URI

[…] Our friends at Ajaxian have a nice article exploring the PHP backend that responds to the AJAX request. I heard a rumor that Karsten is currently working on a sweet PHP backend for WordPress AJAX protocols. Seriously, it sounds dorky (and it is) but it’s gonna be big and it’s gonna be sweet. But don’t take my word for it, ask him yourself. […]

Pingback by FHH3: I’m Outta Here — No One’s Listening Archive — March 24, 2006

[…] Ajaxian » Writing Your Server-Side Ajax Handler (tags: ajax php Programming webdev) […]

Pingback by ARTbird309’s Blog » Article » links for 2006-10-16 — October 15, 2006


I apply a technique called serverside-blocked-calls (server-side AJAX) in my MVC-framework. See demos on my website:

Kind regards,

Stephan Smetsers

Comment by inchl — November 20, 2006

Leave a comment

You must be logged in to post a comment.