XAJAX with PHP – The future of web development

XAJAX is a class library or framework written purely on PHP that allows you to easily create powerful, Web-based, Ajax applications or plugins.

You can download xajax framework from http://sourceforge.net/projects/xajax/

XAJAX is easy to impement. You need to just include XAJAX library, instantiate the xajax object, and register
the name of function (or an object or class method) which you want to call from XAJAX framework. xajaxResponse() is an object to return the results of our requests as XML formats. Then call that particular function you want from xajax framework through your HTML or javascript. That's it. XAJAX takes care of most everything else. You just need to write the PHP functions and returning XAJAX XML responses from them, which is made extremely easy by the xajaxResponse class.


Code:
<?php
include './xajax/xajax_core/xajax.inc.php';

$xajax = new xajax();

$rqstAdd =& $xajax->register(XAJAX_FUNCTION, 'add');
$rqstReset =& $xajax->register(XAJAX_FUNCTION, 'reset');

// set the parameters
$rqstAdd->setParameter(0, XAJAX_JS_VALUE, 39);
$rqstAdd->setParameter(1, XAJAX_JS_VALUE, 571);

$xajax->processRequest();

function doAdd($a, $b)
{
    $response = new xajaxResponse();
    $response->assign('DisplayContents', 'innerHTML', $a + $b);
    $response->assign('reset', 'style.display', 'block');
    return $response;
}

function doReset()
{
    $response = new xajaxResponse();
    $response->clear('DisplayContents', 'innerHTML');
    $response->assign('reset', 'style.display', 'none');
    return $response;
}

<form action="#" method="post">
<input type="button" onclick="<?php $rqstAdd->printScript(); ?>" id="btnAdd" value="Click Here for Submit" />
<input type="button" onclick="<?php $rqstReset->printScript(); ?>" id="btnReset" value="Clear" />
<div id="DisplayContents"></div>
</form>


Processing forms with XAJAX

The xajax.getFormValues() method can be used to automatically process the data from HTML Form asynchronously using Ajax.

xajax.getFormValues() have an argument, which can be either the id of the HTML Form you want to process, or the actual Form object (see Note below).

xajax_processTheForm (xajax.getFormValues('formId'));
where xajax_processTheForm() is the function which you declared and registered with XAJAX that used to call the method xajax.getFormValues().


NOTE: Make sure that you assign your forms an id in addition to a name attribute. Otherwise this technique will not work on some browsers especially on FireFox.

NOTE: For working this technique in Firefox, make sure to assign a name attribute to all input types in the form. Then only the xajax.getFormValues() will work. Just an id attribute will not work.

If you have any comments about this article, please post your comments here.

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net