Tutorials:Learn xajax in 10 Minutes






Tutorials:Learn xajax in 10 Minutes

(Redirected from Learn xajax in 10 Minutes)

View translation in Spanish, Japanese, Russian, Chinese, German, Romanian

[edit] 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.

[edit] 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:

$objResponse = new xajaxResponse();
$objResponse->addAssign("myInput1","value",$DataFromDatabase);
$objResponse->addAssign("myInput1","style.color","red");
$objResponse->addAppend("myDiv1","innerHTML",$DataFromDatabase2);
$objResponse->addPrepend("myDiv2","innerHTML",$DataFromDatabase3);
$objResponse->addReplace("myDiv3","innerHTML","xajax","<strong>xajax</strong>");
$objResponse->addScript("var x = prompt(\"Enter Your Name\");");
return $objResponse;

The xajax message pump would parse the XML message and perform the following:

  • The value of the element with id myInput1 would be assigned to the data in $DataFromDatabase.
  • The color of the text in the element with id myInput1 would be changed to red.
  • The data in $DataFromDatabase2 would be appended to the innerHTML of the element with id myDiv1.
  • The data in $DataFromDatabase3 would be prepended to the innerHTML of the element with id myDiv2.
  • All occurrences of "xajax" in the innerHTML of the element with id myDiv3 would be replaced with "xajax"; making all of the instances of the word xajax appear bold.
  • A prompt would be displayed asking for the user's name and the value returned from the prompt would be placed into a javascript variable named x.

All of this is implemented on the server side in the PHP function by forming and returning an xajax XML response.

[edit] But wait, there's more!

Continue on to Processing Forms with xajax