Tutorial: learn xajax in 10 minutes
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;
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.
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.
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]
Marshall Gatten says:
Uh, yeah. Okay. In that case my comment is that your comment entry script needs some work.
sfsd says:
sfsd
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.
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.
cheap ffxi gil says:
Great to find lots of useful tips and code here. A great article post, this is something very interesting.
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.
