While AJAX may seem scary to a novice programmer, all it really does is connect Javascript to a server-side scripting language of choice. In this article, I’ll show how to easily make use of AJAX with PHP.
The notion of AJAX is very simple to understand. Your browser just makes a request to the web server, just like any other request like as if you were navigating to a page, but instead of reload the page and displaying it, it just returns the markup to a specified Javascript function.
Example: Here
JS Source: ajax.js
PHP Source (as text): ajaxHandler.php
Now the example definitely doesn’t make use of best practices for AJAX (I may write a post about that later, but the gist is that you shouldn’t return HTML or “pretty” stuff, but data that needs parsing or even better, a JSON object to be parsed), but it should give you the basics of how to pass data to PHP and then how to get the response back to Javascript. Now anything you’ll want to do, you just need to know how to do it in either Javascript or PHP.
If you take a look into the ajax.js file, there is a class called ajax. Even though AJAX is quite easy, this class exists so that only one call is needed to make and return an AJAX call. While I won’t go through the details of how the function works, to make an AJAX call with this code, just call ajax.call and pass it three things: the URL to request, the parameters to pass, and the callback function.
The URL should be the URL of the PHP (or whatever) code you wish to request. Note that this code uses POST to request the URL. This can fairly easily be changed to GET if needed. The main different between POST and GET is that POST requires two actual requests to be made per request, while GET has a limited size due to URL length restrictions. When sending form data, POST is recommended while when simply requesting data, GET should be used. Here I use POST to cover all bases.
The parameters should be an object with the property names being the parameter name and their values being the value to pass. Passing null is perfectly acceptable if no parameters are needed.
The callback function is a function that the response from PHP (or whatever) will be given to. This function should have one parameter which will be a String containing the response. Note that this function needs to be passed as an object.
Do note that there will be a slight delay from the time you click on the button to the time when the response is displayed. This is because of the latency of making requests over the internet. It is recommended to disabling buttons while the request is unanswered.
Something to note about AJAX is that it is entirely asynchronous. The ajax.call function is non-blocking, meaning that the code that follows will continue to run even before the request comes back. If you are familiar with event driven programming, this should be an easy thing to understand. If not, be cautious with how your code is set up so that it works no matter when the callback happens. Depending on latency and computer speed, the callback could fire immediately after the ajax.call function is called, well after the rest of the Javascript is run, or anywhere in between. Most browsers implement Javascript as single-threaded, but with AJAX it should be treated as multi-threaded and so good practices for shared objects should be used. While there are no locks in Javascript, a careful programmer can make sure the code works as intended.
While the example does absolutely nothing interesting, it should provide the building blocks for creating your own AJAX code for tying your Javascript and PHP knowledge together.
