Writing Your Server-Side Ajax Handler
Contents:
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.
Set up your Functions
Before you create a handler, you need a set of functions that the handler can reference. Here's an example:
-
<?php
-
function world($params='')
-
{
-
return 'Goodbye Cruel World.';
-
}//end world
-
-
function sweetWorldAction($params='')
-
{
-
//we expect params to be an array or a string similar to a parameter string from a GET..i.e. "bork=asdf&cheese=yes please"
-
//parse out the variables
-
{
-
}//end if
-
-
//do your logic here
-
}//end sweetWorldAction
-
?>
Now that we have the functions all set, we'll set up a handler that receives Ajax calls and calls the appropriate functions.
The Eval Method - A Dynamic Handler
This method is one that I toyed around with for a while and I'll admit that its pretty simple and clean but there are some drawbacks. We're going to make a few assumptions: All requests will be GET (this is to keep things simple for this example) and we will keep the complexity of .htaccess sweetness out of these examples and assume that each call will be passing a function variable that specifies the function we are calling. I'll get to those in a second...but first I'll show you the handler code in this Eval Method:
-
<?php
-
//if the user leaves the page or closes the browser prematurely, this will help prevent half completed statements
-
-
include('functions.php');
-
-
//list out your Ajax accessible functions
-
-
{
-
//get the get parameters
-
$params = $_GET;
-
//unset the function index from the params because we just don't need it there
-
-
//build your parameter string:
-
$param_string='';
-
foreach($params as $key=>$param)
-
{
-
$param_string.=(($param_string)?'&':'').$key.'='.$param;
-
}//end foreach
-
-
//make your function call
-
}//end if
-
-
?>
The Variable Variable Method - A Dynamic Handler
This method (compliments of PHPDeveloper) is simpler than the Eval Method and just as dynamic.
-
<?php
-
//if the user leaves the page or closes the browser prematurely, this will help prevent half completed statements
-
-
include('functions.php');
-
-
//list out your Ajax accessible functions
-
-
{
-
//get the get parameters
-
$params = $_GET;
-
//unset the function index from the params because we just don't need it there
-
//make your function call
-
$_GET['function']($params);
-
}//end if
-
-
?>
The Switch Method - A Basic Handler
This method handles each call individually. The reason for using switch rather than if-elses is simply because your application will most likely possess a large number of Ajax-ified functions and those if-elses would be ghastly to read and decrease performance...the Switch statement is much cleaner.
Make a Choice
Which are you going to use? While the Eval and Variable Variable Methods contain very small amounts of code in the handlers, the logic has simply been shifted to the functions themselves. Increased Ajax-geared logic within the functions themselves reduces portability. However, in the Switch Method, logic is organized in a fairly easy to follow manner while making use of very generic functions that can be used in multiple fashions. Obviously, the Switch Method is my preferred choice :)
In Closing
Handler scripts make the Ajax magic happen and the separation of handler logic from your application logic is just as important for robust development and debugging as the need for separation of layout and logic. Play around with the above methods and see which works for you. If you have a method all your own, I'd be interested to hear it!
Discuss This Article
|
|
19 Responses to “Writing Your Server-Side Ajax Handler”
-
Zach
Posted: Mar 22nd, 2006 at 6:28 am1 -
enygma
Posted: Mar 23rd, 2006 at 12:34 pmReply to this comment.grr….it apparently interpreted the PHP from the las one, so here it is again:
If you didn’t want to use eval (remember, eval’s only one letter away from evil), you can always use variable variables:
[php]
function test($parms){
print_r($parms);
}//be sure to filter the input first!
$filtered=filterThisInput($_GET);$filtered['function_called']($filtered);
[php]calling the page with: page.php?function_called=test&foo=1&bar=2
would result in:Array (
[function_called] => test
[foo] => 1
[bar] => 2
)2 -
Jim Plush
Posted: Mar 23rd, 2006 at 9:46 pmReply to this comment.Matt, you should check out the my-BIC php/ajax framework. There is one server file mybic_server.php that pretty much does what you’re looking for, it also supports GET/POST JSON/TEXT/XML request/responses in less than 100 lines of code.
http://www.litfuel.net/mybic4 -
sp
Posted: Mar 24th, 2006 at 6:16 amReply to this comment.I think one function for all ajax requests is not a very good idea. First, you have to invent those methods (btw, I always use ’switch’ if I have to create something like that, my non-ajaxed blog-engine for instance), second this makes your script a bit more complicated. However, maybe, it`s just a question of style.
I prefer another way: for each request I make file, which name consits of ‘xhr’ prefix and the name of the ajax function that ewokes it. So, for the javascript function loadCommentsData with xmlhttprequest inside, I create ‘xhr.loadCommentsData.php’ file to process it.
5 -
sp
Posted: Mar 24th, 2006 at 6:18 amReply to this comment.Damn. Sorry for mistakes and my whole knoledge of english. :)
6 -
Jared White
Posted: Mar 25th, 2006 at 10:12 amReply to this comment.That’s a good start for an Ajax server in PHP. However, as I’ve found, it’s often necessary to have a more advanced response with various commands acting on incoming data, and it’s nice to be able to use PHP to push those commands to the client. My approach is to use something like xajax (which I’m a co-developer of, actually :) ) which lets you register functions or object methods, call them from Javascript with a simple function call, and then you can craft a response using a number of different commands via an easy-to-use response object in PHP.
Check it out — it’s kind of neat, I think.
8 -
trackback:
Posted: Mar 26th, 2006 at 5:56 amAnonymous Reply to this comment.BorkWeb Writing Your Server-Side Ajax Handler…
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 …
9 -
Chris
Posted: Jul 6th, 2006 at 2:07 pmReply to this comment.this maybe a more modular way to call to seperate classes
$class =$_GET['class'];
include_once($_SERVER['DOCUMENT_ROOT'].”/classLib/$class.class.php”);
$obj = new $class();
$methods = get_class_methods($obj);
if (in_array($_GET['method'], $methods)) {
$params = $_GET;
unset($params['method']);
unset($params['class']);
$obj->$_GET['method']($params);
}else{
die(’Invalid call!’);
}10 -
Nival
Posted: Sep 24th, 2006 at 4:46 amReply to this comment.Cool project!
11 -
Jean
Posted: Sep 27th, 2006 at 5:40 amReply to this comment.Good site!
Bye.12 -
pingback:
Posted: Oct 10th, 2006 at 10:00 amBorkWeb » The Ajax Experience Reply to this comment.[...] MasterWish - a one and a half year old brainchild of a couple of other developers and myself - has been my playground after work hours. I began my experimentation with Ajax using that site, as PSU tended to slap my hand when looking into Ajax at work. Through that experimentation, I began to understand the workings of Ajax and how it would work into the PHP/Templating environment I was so comfortable with. Once I had dug down, I promptly posted my “Ajax; Templating; and the Separation of Layout and Logic” article that Ajaxian blogged about, along with the follow-up article. [...]
13 -
angelsoft
Posted: Oct 20th, 2006 at 9:47 amReply to this comment.interesting ajax tips.
14 -
hot-asian-babes
Posted: Mar 23rd, 2007 at 8:12 amReply to this comment.Thanks for your great site!
15 -
Coble
Posted: Mar 30th, 2007 at 11:49 pm16 -
AaronMC
Posted: Nov 26th, 2007 at 3:46 pmReply to this comment.I was looking at your code.. its a great idea, however I was wondering why you dissected the $_GET variable in your dynamic handler when you could have simply passed the $_GET array to the ajax function (after unsetting the ['function'] key). ie.
$params = $_GET;
//unset the function index from the params because we just don’t need it thereunset($params['function']);
eval($_GET['function'].”($params);”);I was just curious because maybe im missing a good reason that I didn’t see. It just seems that you are doing twice the amount of work.
17 -
AaronMC
Posted: Nov 26th, 2007 at 3:49 pmReply to this comment.Sorry, I just realized this is equal to your posted Variable Variable Method!
18 -
AaronMC
Posted: Nov 26th, 2007 at 3:51 pmReply to this comment.I also wanted to let you know I am working on a version of this code to get $_POST variables to work as well since HTTP GET has a max header size and will not work for sending larger blobs of data! If anyone has any suggestions please post!
19



Another reason to use switch over if/else is performance. Switch statements are highly optimized for a lot of conditions.