Event-Based AJAX Framework for PHP : Part 2

Posted on May 29, 2008
Filed Under AJAX, JavaScript, PHP | 7 Comments

Articles in this series:

  1. Introduction to the Framework
  2. Problems with Creating an Event Based AJAX Callback Mechanism
  3. JSON data passing
  4. The Scriptifiable Interface
  5. The Remotable Interface

This is the second part to the PHP AJAX Framework series. In this article I will discuss the problems involved in creating an event-based ajax callback mechanism and how to get around them. 

The biggest issue with creating a versitile event based AJAX callback system is figuring out how to return executable script back from the server. There are a few ways to do that, but what I feel is the easiest of them (and the one I used in creating this framework) is the iFrame-AJAX method.

To perform an AJAX call with an iFrame, you start off by putting a 0px by 0px iframe on your page. Just to be sure no one can see it, I set the visibility CSS property to hidden. After that’s in place, you can have a javascript function available to update the src tag of your iFrame, forcing it to reload.

The big difference between this and an XMLHttpRequest call is that this forces the browser to parse and run any javascript returned to that hidden iFrame. This is crucial because any script you add to your iFrame’s onload event will fire once the content is loaded. This essentially means that all processing on the server is completed and you can notify your user that something has happened (or if an error occurred in the process).

Here’s how you might implement iFrame AJAX:

Inside the Remoter JavaScript Class

At start up, this client-side class needs to set up the iFrame that it will use to communicate with the server. I haven’t figured out another way to do this yet, but in order for the script running in the iFrame to know what the variable is for the Remoter instance, I chose to pass this in as a string on construction.

After that is all set, we can make calls to it using the RemoteInvoke method. This method takes in 4 parameters: the Remotable PHP Class name, the static function in that PHP class that you are calling, an array of parameters, and a reference to the javascript callback function.

The Remotable PHP Class

For now, we’ll just assume all classes fall into this category. By convention, we’re assuming your classes have file names that follow this pattern: ClassName.class.php. You will pass “ClassName” as a string into the first parameter space of your RemoteInvoke call.

The Static PHP Function

This is just a static method in the Remotable PHP class. Nothing special. Just like your ClassName parameter, you’ll pass this in as a string to your RemoteInvoke call.

Array of Parameters

You can pass this in either as a variable pointing to a new Array(params, go, here) or to just put new Array(params, go, here) right in the function call.

Javascript CallBack Function

This function will be called immediately after the server-side code has completed executing and the results are returned. The callback function will be called with your return value as an argument, if there is one. The return value will be null otherwise.

Making the Call

To make the AJAX call, Remoter needs to formulate a query string that is comprised of all the stuff needed to execute the call on the server side. The query string is passed to a PHP file called Remotee.php that gets loaded in the iFrame after being processed, and in the end, executes the callback. Below is a remoter.RemoteInvoke function call and the resulting string that the iFrame’s src is set to:

The RemoteInvoke Call

remoter1.RemoteInvoke(”MathClass”, 
                     “AddFunction”, 
                     new Array(1, 2), 
                     function(result) {
                        alert(result); 
                     });

The Resulting iFrame src Tag

 iframe.src = "remotee.php?rand=0.393858&remoter=remoter1&params[]=1&params[]=2&class=MathClass&call=AddFunction";

We pass the params in as an array of values, the class and function call make sense, but what is the rand for?

Rand

There’s always a chance, when dealing with iFrames (these are just web pages), that your browser will cache and redisplay content if it thinks it doesn’t need to re-get it. To trick your browser into always thinking the content is new, we get a random number and throw it in the query string. It is reasonable to say that this makes the iFrame src tag unique always.

What is Remotee.php

The Remotee script does the following:

  1. Get the desired PHP class.
  2. Call the requested function in that class.
  3. Return an HTML page that, onload, executes the CallBack function with the return value as its parameter.

Nothing incredibly special here.

Get the Source

I’m including the entire source in this article. If improvements are made between now and the time the series is finished, it will be updated accordingly. I will also link to it in subsequent postings. If you have any questions about what was covered in this post, please feel free to ask. If you have questions about pieces of the framework that were not yet covered, drop me a note and I’ll make sure they get answered.

Download the PHP-AJAX Framework

What’s next?

The next post in this series will cover how we use JSON to pass more complex data structures from PHP to JavaScript and JavaScript back to PHP.

Comments

7 Responses to “Event-Based AJAX Framework for PHP : Part 2”

  1. Event-Based AJAX Framework for PHP : Part 3 : Jake’s Thoughts on Tech on June 1st, 2008 3:58 pm

    [...] the last article in this series, I talked about how the RemoteInvoke method worked and explained how you pass parameters to your [...]

  2. Pierre Paul Lefebvre on June 12th, 2008 2:02 pm

    I recently decided to make an event base apps in PHP. An app displaying custom surveys that need to display warnings if some dynamic questions are selected. So I went for the events-based system. The system is still in developpement, but as of now Im not using any iFrame. Im using a singleton pattern for my EventHandler. In my getInstance of my EventHandler Im trying to pull off first EventHandler that is stored in the $_SESSION before making a new instance. Thanks for your article tho, its great to see I wasnt the only one thinking about this :)

  3. Event-Based AJAX Framework for PHP : Jake’s Thoughts on Tech on June 13th, 2008 1:12 pm

    [...] Problems with Creating an Event Based AJAX Callback Mechanism [...]

  4. Event-Based AJAX Framework for PHP : Part 5 : Jake’s Thoughts on Tech on June 13th, 2008 1:13 pm

    [...] Problems with Creating an Event Based AJAX Callback Mechanism [...]

  5. Event-Based AJAX Framework for PHP : Part 4 : Jake’s Thoughts on Tech on June 13th, 2008 1:16 pm

    [...] Problems with Creating an Event Based AJAX Callback Mechanism [...]

  6. bQ23Kristin on January 16th, 2010 5:58 pm

    The essay writing just about this good post, I can notice at the paper writing service. Purchase the research papers or custom writing just about this post.

  7. College Paper on January 26th, 2010 1:36 pm

    I believe the information covered in the discussion is top notch. I’ve been doing a research on the subject and your blog just cleared up a lot of questions. I am working on a custom research paper and custom research papers for my English class and currently reading lots of blogs to study.

Leave a Reply




Captcha
← Enter these letters.