Version: 1.0.0a


LibAjax is a library that allows the use of server side and client side scripting using XMLHTTPRequest (Named AJAX). LibAjax makes using Ajax even easier. And released under the is released under the GNU LGPL license, so it may be redistribute with you're scripts, used on your sites, or anything else that falls under the license.

LibAjax currently has a PHP backend, but there are plans for a ASP backend in the future.

Anyone wishing to learn more about ajax, before using this library, might find the wikipedia entry interesting, or the essay that brought ajax to light.

The guide below will teach you how to take use of the user side of libajax, any developers that would like to learn more can check out our sourceforge page, and our phpdoc generated documentation. However, the end users downloading this script should at least know a little about Javascript and PHP, but we also have forums hosted by sourceforge at the project page to offer support.



There are really no steps to installing LibAjax, however you will need to download the latest libajax.php file from the file releases section at the sourceforge page.

After downloading LibAjax, upload it to your webhost to the root directory, which is the directory where you might find your index.php, or index.html file.


Making our first script


In the PHP file you wish to use LibAjax with, call to it and create a new instance of libajax:

$ajax = new ajax();

We then need to create our php functions to export. For this example we will do a simple multiply with PHP.

function multiply($a, $b) {
print $a * $b;

And the next three lines setup the library to do the processing.

$ajax->mode = "POST";
$ajax->export = array("multiply");

The first line sets which mode to send the data by. This can be either GET or POST. GET Should be used to view data, and POST should be used to send long strings of data or to edit or change data. More information can be read at the rfc section 9 specs. In this case, I've used POST, but GET should work here too.

The next line tells us which functions to export. You can add more to the array by seperating each function name with a comma. In this case, we only need the php function, multiply.

The final function must be called, and will call back to the functions, as well as set the POST and GET variables for later use.

The JavasScript & HTML

First we will need to start the javascript tag to be able to output all of it, as well as create our own, this can be any number of ways, but I've embedded the javascript into HTML. (Rather then using an external dummy file, or any other ways).

<title>Multiply Script</title>
<script type="text/javascript">
<?php $ajax->output(); ?>

The first four lines should be obvious for anyone that knows HTML, but the line after is a php statement, that will output all the generated javascript.

The next bit of code will be are custom javascript, that will take use of the wrapper function.

function multiply_init(answer) {
document.getElementById("c").value = answer;

function multiply() {
var a = document.getElementById("a").value;
var b = document.getElementById("b").value;

This is where it gets confusing. The multiply_init, or this type of dummy function, is used to send data back to be used in the HTML source code, sure it will call to the php, but it also needs to send back the data for example, in this case. The multiply_init function is getting the answer from the php function, and then setting it to an ID. This will be called in a short while.

The multiply function is what we should call to activate everything. The first two lines are just setting some variables, but the third line calls to the wrapper function for multiply, all the functions that have been exported are prefixed with ajax_, and the first two args are beeing passed from a form. The las one, we see multiply_init, which will then be used as a callback to send us data to display.

And now the form, which is pretty easy to figure out, we are naming the forms and giving them ID's which are used in the multiply and multiply_init functions. And we use an onclick method to call to multiply when the form is called.




<input type="text" name="a" id="a" value="5" size="5"> * <input type="text" name="b" id="b" value="5" size="5"> =
<input type="text" name="c" id="c" value="" size="5">
<input type="button" name="check" value="Math" onclick="multiply(); return false;">


Now upload this script to the same directory as libajax, or change the require_once statement to call to the libajax file, and name it multiply.php, give it a try as your first AJAX and LibAjax script. You can also try a demo here.


Now what?

Try making some other scripts or trying out some of our examples. Or even get some free traffic by telling us about them (directions listed on the examples page). Due to the open source nature of this project, you can edit and even redistribute the library as long as the orginal credit goes to libajax/Justin Shreve, and you follow the rules of the GNU LGPL license.

You can also download the phpdoc generated documentation to learn more about the coding side of the library.

Top Logo