XAJAX library

Today, I want to talk about my favorite AJAX library, XAJAX. According to Wikipedia:

xajax is an open source PHP class library implementation of AJAX that allows developers to create web-based Ajax applications using HTML, CSS, JavaScript, and PHP. Applications developed with xajax can asynchronously call server-side PHP functions and update content without reloading the page.

Unlike some other Ajax frameworks, xajax is designed to allow the programmer to have no prior knowledge of JavaScript

Basically, for PHP programmers like me, XAJAX provides an easy way to add AJAX functionality without having to hack with too much JavaScript and XMLHttpRequest objects.

For those who don”t what AJAX is, check out this article.

Steps to get XAJAX working

    1. Follow the directions at XAJAXProject to install XAJAX on your web server (very easy)
    2. Add code similar to this in your php file:
//make obj
$xajax = new xajax();
//register php functions to be AJAXed (see later)
$xajax->registerFunction("xcheckSubmission");
//process requests
$xajax->processRequests();
//check form submission validation
function xcheckSubmission($data)
{
    //do validation here
    //....
    //set a variable to what you want to display asynchronously
    $msg = "Valid!";
    //make response obj
    $objResponse = new xajaxResponse();
    //set response - here, innerHTML of element with id=submit_status will be set to $msg
    $objResponse->addAssign("submit_status", "innerHTML", $msg);
    //return response
    return $objResponse;
}

Here is the javascript call that will call our AJAXed php function:

//notice "xajax_" prefix
<input onclick="xajax_xcheckSubmission()" type="button" name="submit" value="Submit" />

That’s it!

This entry was posted in Software, Tech. Bookmark the permalink.

Leave a Reply

Your email address will not be published.