XAJAX In 7 Steps

December 29th, 2008 Leave a comment Go to comments

Using xajax in a PHP script

xajax is designed to be extremely easy to implement in both existing web applications as well as new projects. You can add the power of xajax to nearly any PHP script in seven easy steps:

1. Include the xajax class library:

require_once("xajax.inc.php");

2. Instantiate the xajax object:

$xajax = new xajax();

3. Register the names of the PHP functions you want to be able to call through xajax:

$xajax->registerFunction("myFunction");

4. Write the PHP functions you have registered and use the xajaxResponse object to return XML commands from them:

function myFunction($arg)
{
// do some stuff based on $arg like query data from a database and
// put it into a variable like $newContent

// Instantiate the xajaxResponse object
$objResponse = new xajaxResponse();

// add a command to the response to assign the innerHTML attribute of
// the element with id="SomeElementId" to whatever the new content is
$objResponse->addAssign("SomeElementId","innerHTML", $newContent);

//return the  xajaxResponse object
return $objResponse;
}

5. Before your script sends any output, have xajax handle any requests:

$xajax->processRequests();

6. Between your <head></head> tags, tell xajax to generate the necessary JavaScript:

<?php
// Also add the Xajax subdirectory name as an argument if you have the Xajax files, etc. within a subdirectory
$xajax->printJavascript(); ?>

7. Call the function from a JavaScript event or function in your application (you must change SomeArgument into a real variable, a string, etc.–some argument is required):

<div id="SomeElementId"></div>
<form action="get">
<input type="button" value="Insert" onclick="xajax_myFunction(SomeArgument);" />
</form>

That’s it. xajax takes care of most everything else. Your biggest task is writing the PHP functions and returning xajax XML responses from them– which is made extremely easy by the xajaxResponse class.

  1. Prof. Jose Delgado
    January 19th, 2010 at 08:55 | #1

    Excelente información para quien quiere aprender sobre cómo utilizar xajax…

  2. February 4th, 2011 at 21:23 | #2

    Hi, good night friends, yours framework’s very good, I’m brazillian, i saw the framework in very famous website of tutorials in brazil,
    Video Aulas Brasil, I liked, congratulations for the very good job.

    Thank’s for attention and sorry for my bad english.

    Att.WilliamF CLGames.
    PortoAlegre, 5 de fevereiro de 2011

  3. Persa
    August 14th, 2011 at 02:51 | #3

    Hi,
    xajax how to work with cache, gzip, etag ??! any class ? any idea ?

    Thanks

  1. No trackbacks yet.