PHP and AJAX Just Got A Little Easier.

I started work on a little open source project the other day which aims to simplify using AJAX in your PHP applications. I call it php2ajax and it’s hosted over at GitHub.

I frequently see issues on StackOverflow stemming from the nuances of processing GET/POST variables in PHP and the overall complexity the interface brings to the plate of a beginner. I thought to myself, why not encapsulate these common problems and make life easier for everyone? With that, php2ajax was born.

So let’s look at a simple example on using it. Here is our HTML/jQuery page called index.html:

We have two elements on our page, a link and a div box. A jQuery function is setup to submit an AJAX request to test.php when the link is clicked. When it receives a return from the PHP script, it places it in the DIV. Easy day right?

Now normally in PHP, we would have to check for $_GET or $_POST vars, filter the input, maybe store it in some variables, pass it around and do things with it, etc. This process can be cumbersome and start to make your code look more like a dish straight out of an Italian restaurant (spaghetti anyone?). Let’s see how simple it is with php2ajax in our test.php file:

Cool huh? In addition to the hasRequest flag, there are also hasPost and hasGet flags for more specific handling which are available after making the getRequest() call.

The filter() method takes an array of function names (as strings) which accept single values to process and return the modified version. You also have the option of writing your own custom filter method and passing in the name of that as well:

Another cool feature is the save() method. This method is used to pass filtered GET/POST data to an object of your choosing for saving. For example, lets say we have a custom class called myDatabase that has a method called insert(). We call it like this:

The third parameter is data you wish to pass to the function. You could just as easily pass the entire php2ajax object to your data handling class to be processed there.

And finally, I am currently implementing a doLongPoll() method which does exactly as the name implies. If you haven’t heard of long polling, it is a form of push technology often referred to as Comet. Wikipedia describes it as:

Long polling is a variation of the traditional polling technique and allows emulation of an information push from a server to a client. With long polling, the client requests information from the server in a similar way to a normal poll. However, if the server does not have any information available for the client, instead of sending an empty response, the server holds the request and waits for some information to be available. Once the information becomes available (or after a suitable timeout), a complete response is sent to the client. The client will normally then immediately re-request information from the server, so that the server will almost always have an available waiting request that it can use to deliver data in response to an event. In a web/AJAX context, long polling is also known as Comet programming.

So that will be implemented soon. What other tedious tasks do you encounter frequently using PHP and AJAX which could be simplified using this object oriented class? Leave me a comment below and I will see about implementing it.

How to Pass PHP Variables to Javascript

Passing variables or data from PHP to Javascript is a popular topic on StackOverflow. Not because it is debatable or the idea merits involved discussion, but because many people either do not research their question and expect others to solve their problems for them (which I will touch on another day) or have been unable to understand existing examples.

So today I’m going to tackle this question and hopefully alleviate some of the confusion.

Continue reading “How to Pass PHP Variables to Javascript” »