Tutorial: learn xajax in 10 minutes

DOCS & TUTORIALS » learn xajax in 10 minutes

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:

Code: php

 
require_once("xajax_core/xajax.inc.php");
 



2. Instantiate the xajax object:

Code: php

 
$xajax = new xajax();
 



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

Code: php

 
$xajax->registerFunction("myFunction");
 



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

Code: php

 
function myFunction($arg)
{
    // do some stuff based on $arg like query data from a database and
    // put it into a variable like $newContent
        $newContent = "Value of \$arg: ".$arg;
    
    // 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->assign("SomeElementId","innerHTML", $newContent);
    
    //return the  xajaxResponse object
    return $objResponse;
}
 



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

Code: php

 
$xajax->processRequest();
 



6. Between your tags, tell xajax to generate the necessary JavaScript:

Code: php

 
<?php $xajax->printJavascript(); ?>
 



7. Call the function from a JavaScript event or function in your application:

Code: html

 
<div id="SomeElementId"></div>
<button onclick="xajax_myFunction('It worked!');"></pre>
 


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.

How do I update my content asynchronously?

Perhaps the most unique feature of xajax is the xajaxResponse class. Other Ajax libraries require you to write your own callback handlers in Javascript to process the data returned from an asynchronous request and to update the content. xajax, on the other hand, allows you to easily control your content from PHP. The xajaxResponse class allows you to create XML instructions to return to your application from your PHP functions. The XML is parsed by xajax message pump and the instructions tell xajax how to update the content and state of your application. The xajaxResponse class currently offers a number of useful commands, such as Assign, which sets the specified attribute of an element in your page; Append, which appends data to the end of the specified attribute of an element in your page; Prepend, which prepends data to the beginning of the specified attribute of an element in your page; Replace, which searches for and replaces data in the specified attribute of an element in your page; Script, which runs the supplied JavaScript code; and Alert, which shows an alert box with the supplied message text.

A single XML response may contain multiple commands, which will be executed in the order they were added to the response. For example, let's say that a user clicks on a button in your application. The onclick event calls the Javascript wrapper for a PHP function. That wrapper sends an asynchronous request to the server through XMLHttpRequest where xajax calls the PHP function. The PHP function does a database lookup, some data manipulation, or serialization. You use the xajaxResponse class to generate an xajax XML response containing multiple commands to send back to the xajax message pump to be executed:

Code: PHP

 
$objResponse = new xajaxResponse();
$objResponse->assign("myInput1","value",$DataFromDatabase);
$objResponse->assign("myInput1","style.color","red");
$objResponse->append("myDiv1","innerHTML",$DataFromDatabase2);
$objResponse->prepend("myDiv2","innerHTML",$DataFromDatabase3);
$objResponse->replace("myDiv3","innerHTML","xajax","<strong>xajax</strong>");
$objResponse->script("var x = prompt(\"Enter Your Name\");");
return $objResponse;
 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY obj_created asc' at line 57
 
Friday, August 24, 2012 21:53

Allen says:

ridiculous! your example on home page even not working! how can you put a buggy example on your home page without testing it. i don\'t see the prospect of xajax.

Friday, November 9, 2012 05:14

Marshall Gatten says:

Your Step 3 is incorrect. I assume it\'s for an older version. It current shows the syntax as: $xajax->registerFunction(\"myFunction\"); It should be: $xajax->register(XAJAX_FUNCTION, \'myFunction\'); There is no reegisterFunction() to call and so it will error out if you follow these directions.

Friday, November 9, 2012 05:16

Marshall Gatten says:

[CODE=php] Let\'s try that again, with code tags since it ran all my lines together. Your Step 3 is incorrect. I assume it\'s for an older version. It currently shows the syntax as: $xajax->registerFunction(\\\"myFunction\\\"); It should be: $xajax->register(XAJAX_FUNCTION, \\\'myFunction\\\'); There is no reegisterFunction() to call and so it will error out if you follow these directions. [/CODE]

Friday, November 9, 2012 05:18

Marshall Gatten says:

Uh, yeah. Okay. In that case my comment is that your comment entry script needs some work.

Friday, February 8, 2013 16:39

sfsd says:

sfsd

Monday, February 18, 2013 15:01

odzyskiwanie danych says:

In like manner numerous rewards ready to you, it is pure joy to play this amusement. You get significantly more fervor further life into this diversion. In it you can revel in Ancient Egyptian subject from the time of the Pyramids and Tutankhamuntheme and incredible player compensates accessible, by its diverse topic it progress exceptionally much notoriety.

Monday, March 11, 2013 13:04

kavita says:

Read here As increasingly programming associations keep agitating out their last productions, some attempt to be more involved and special than the final, although alternates have a winning recipe and basically mass prepare spaces that are truly comparable, and just have minor contrasts in the subjects they accord.

Tuesday, March 26, 2013 07:59

cheap ffxi gil says:

Great to find lots of useful tips and code here. A great article post, this is something very interesting.

Wednesday, April 3, 2013 13:49

CATICH says:

styrofoam I have visited to this site multiple times and everytime I find useful jobs for me so I would suggest please come to this site and take the chance from here.

 
DOCS & TUTORIALS » learn xajax in 10 minutes


XAJAX on SourceForge.net
Support this project Valid XHTML 1.0 Transitional