10 minutes to learn Xajax

Source: Internet
Author: User
Tags add object end functions variables php script variable wrapper
Ajax

The translator presses: xajax The biggest characteristic is he uses the XML response, thus we can use the PHP to decorate, handles the asynchronous transmission data, the webpage content update. These other Ajax frameworks are done by JS. Xajax allows us to write just a few PHP functions that can be implemented.
All the key to learn Xajax is to Master Xajaxresponse class.

Tutorials:learn Xajax in minutes
Tutorial: 10 minutes to learn Xajax

Using Xajax in a PHP script
A PHP script that uses the Xajax:

Include the Xajax class library:
Call the Xajax class library:

Require_once ("xajax.inc.php");

Instantiate the Xajax object:
Instantiating a Xajax object

$xajax = new Xajax ();

Register the names of the PHP functions you want to is able to call through Xajax:
Register a PHP function name that you want to invoke with Xajax (corresponds to the function name in JavaScript xajax_myfunction)

$xajax->registerfunction ("MyFunction");

Write the PHP functions you have registered with the Xajaxresponse object to return XML commands from them:
Write the PHP function that you have just registered and use the Xajaxresponse object to return the XML instruction set

function MyFunction ($arg)
{
Do some stuff based on $arg like query data from a database and
Put it in a variable like $newcontent
Do some basic operations on parameter $arg such as: After fetching data from the database and defining it to the $newcontent variable

Instantiate the Xajaxresponse object
Instantiating a 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
Add a command in the response instance to use the innerHTML element attribute with ID Someelementid
into any new content.
$objresponse->addassign ("Someelementid", "innerHTML", $newcontent);

Return the XML response generated by the Xajaxresponse object
Returns the XML response generated by the Xajaxresponse object
return $objresponse->getxml ();
}

Before your script sends any output, have xajax handle any requests:
Xajax to process all requests before your script sends out anything.

$xajax->processrequests ();

Between your tags, tell xajax to generate the necessary javascript:
Insert the following code between the and tabs of the page so that the Xajax instance can generate the required JS on its own

printjavascript ();?>

Call the function from a JavaScript event or function in your application:
The relative response function you have registered before the JS event or function call in your program


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 are made extremely easy By the Xajaxresponse class.
Just these steps. Others to Xajax to deal with it. Your main task is simply to write the functions in PHP so that they can return the Xajax XML response, and this step can be easily solved with the Xajaxresponse class.

How do I update my content asynchronously?
How do I update my content asynchronously?

Perhaps the most unique feature of Xajax is the Xajaxresponse class. Other AJAX libraries require your to write your own callback handlers in JavaScript to process the data returned from Ynchronous request and to update the content. Xajax, on the other hand, allows your to easily control your content from PHP. The Xajaxresponse class allows you to create XML instructions to return to your application from your PHP. The XML is parsed by Xajax message pump and the instructions tell Xajax the content and state of your applic ation. The Xajaxresponse class currently offers a number of useful commands, such as assign, which sets the specified attribute O F 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 the ' 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 text.
Perhaps the most unique advantage of Xajax is the Xajaxresponse class. Other AJAX libraries require you to write a handle to the callback with JS, to handle an asynchronous request and get the data and update its contents. On the other hand, Xajax simply controls the content of PHP. Then through the Xajaxresponse class, make the XML instruction in your PHP function return to your program. XML will be parsed by Xajax information (pump). Its instructions tell Xajax how to update the content and the location in your program. Now Xajaxresponse has provided a large and helpful command:http://www.flaspx.com/weblog/blog.php?bid=16(Slightly ...) Pay a detailed Xajaxresponse class description)

A single XML response may contain multiple commands, which'll be executed in the order they were to the added. For example, let's say that's a user clicks on the a button in your application. The onclick event calls the JavaScript wrapper for a PHP function. That wrapper sends a 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. Use the Xajaxresponse class to generate a xajax XML response containing multiple commands to send back to the xajax m Essage pump to be executed:
A single XML response can contain more than one command, and they will be executed according to the order in which they are added to the response. For example, let's say that a user presses a button in your program. This pressed event will call the PHP function that is encapsulated by JS. This packet sends an asynchronous request to the server via XMLHttpRequest, allowing Xajax to invoke the PHP function. This PHP function does a query database, some data processing or sorting operations. And you're going to use the Xajaxresponse class to produce a xajax XML response that contains more than one command. Send to Xajax information pump to perform:

$objresponse = new Xajaxresponse ();

$objresponse->addassign ("MYINPUT1", "value", $datafromdatabase);
$objresponse->addassign ("Myinput1", "Style.color", "Red");
$objresponse->addappend ("Mydiv1", "innerHTML", $datafromdatabase 2);
$objresponse->addprepend ("Mydiv2", "innerHTML", $datafromdatabase 3);
$objresponse->addreplace ("Mydiv3", "innerHTML", "Xajax", "xajax");
$objresponse->addscript ("var x = prompt (\" Enter your name\); ");

return $objresponse->getxml ();

The XAJAX message pump would parse the XML and perform the following:
xajax information pump resolves the following XML information and performs the following actions:

The value of the element with ID MYINPUT1 would is assigned to the data in $datafromdatabase.
Assign the variable $datafromdatabase to the value element with the ID myinput1.

The color of the text in the element with ID MYINPUT1 would is changed to red.
Font color elements with ID MYINPUT1 will be replaced with red.

The data in $datafromdatabase 2 would is appended to the innerHTML of the element with ID mydiv1.
$datafromdatabase 2, this data will be appended to the end of the INNERTHML element with ID MYDIV1

The data in $datafromdatabase 3 would is prepended to the innerHTML of the element with ID mydiv2.
$datafromdatabase 3, this data will be added to the beginning of the INNERTHML element with ID mydiv2

All occurrences to "Xajax" in the innerHTML of the element with ID Mydiv3 would is replaced with "Xajax"; Making all of the instances of the word xajax appear bold.
All the "Xajax" in the innerHTML element with ID Mydiv3 will be replaced with "xajax" to make all xajax appear in bold.

A prompt would is displayed asking for the user's name and the value returned from the prompt to would into a placed Cript variable named x.
There will be an input box that pops up and asks for the user's name. The variables obtained from the input box are converted to JS variables and named X.
All are implemented on the "server side in" PHP function by forming and returning a xajax XML response.
All of these are composed of PHP functions that are executed on the server side and then an XML response is returned.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.