Xajax中文手册(第一版)
出处:
本站整理 作者:佚名 日期:2007-5-7 12:12:09 人气: [
大
中 小]
一个独立的XML响应可能包含多个指令, 他们将按照加入响应的顺序执行. 让我们用一个用户在你的程序之中点击按钮为例来进行说明. Onclick事件调用PHP函数对应的javascript封装.这个封装通过XMLHttpRequest发送异步请求到服务器给xajax调用PHP函数. PHP函数做了一次数据库查询, 处理了一些数据, 或者序列化. 然后你使用xajaxResponse类生成包含多个指令的xajax的XML响应 ,并回送给xajax的消息分析器执行:
A single XML response may contain multiple commands, which will 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->getXML();
$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->getXML();
xajax消息分析器将会解析XML消息,并执行以下工作:
- id为myInput1的元素的值将被赋值为 $DataFromDatabase的数据.
- id为myInput1的元素的颜色将会变为red.
- $DataFromDatabase2的数据会被追加到id为myDiv1的元素innerHTML之中.
- $DataFromDatabase3的数据会被预先赋值给id为myDiv2的元素innerHTML之中.
- id为myDiv3的元素的innerHTML 之中所有的"xajax"将被替换为"<strong>xajax</strong>"; 使得所有的单词 xajax 显示加粗.
- 一个提示框将会显示,用来询问用户姓名,从提示框返回的值会被命名为x的javascript变量接收.
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 "<strong>xajax</strong>"; 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.
所有这些都由构成的PHP函数在服务器端执行并返回xajax的XML响应.
All of this is implemented on the server side in the PHP function by forming and returning an xajax XML response.
如何异步处理表单数据?
Xajax使得异步处理表单数句非常非常的简单. xajax.getFormValues()方法会自动的从表单提取数据,并作为一个参数提交给xajax注册的PHP函数.
xajax.getFormValues() 仅仅需要一个参数, 可以是你需要处理得表单的id, 或者是一个实际的表单对象. 你也可以使用xajax.getFormValues作为一个参数给xajax 函数, 例如:
xajax_processFormData(xajax.getFormValues('formId'));
xajax 会生成一个与表单数据对应的请求字符串给xajax服务器解析,然后以一个与表单数据对应的数组传递给PHP函数,就想你提交表单使用PHP的$_GET数组那么简单.
Xajax可以处理类似普通多维数组或者联合数组(哈希数组)等形式的复杂输入名字. 例如, 如果一个表单有三个多选框(checkboxes)并且都命名为 "checkbox[]", 但是值分别为 "check1", "check2", 和 "check3", 然后使用 xajax.getFormValues 函数作为参数传递给xajax 函数, 则 PHP 函数会接受到一个如下的数组:
array (
'checkbox' =>
array (
0 => 'check1',
1 => 'check2',
2 => 'check3',
),
)
作为函数参数的数组的结构与传统意义上提交表单之后的$_GET数组的结构相同. 你可以访问数组之中的checkbox 的数据: $aFormData['checkbox'][0]
How do I process form data asynchronously?
xajax makes processing form data asynchronously extremely easy. The xajax.getFormValues() method can be used to automatically extract the data from a form and pass it as a parameter to a PHP function you have registered with xajax.
xajax.getFormValues() takes one argument, which can be either the id of the form you want to process, or the actual form object. You use xajax.getFormValues as a parameter to your xajax function, like this:
xajax_processFormData(xajax.getFormValues('formId'));
xajax generates a query string representing the form data which is parsed by the xajax server and passed to your PHP function as an array representing the form data, just as if you had submitted the form and used the PHP $_GET array.
xajax will even handle complex input names to generate multidimensional and associative arrays. For instance, if you have a form with three checkboxes and you give them all the name "checkbox[]", but different values like "check1", "check2", and "check3", and you use the xajax.getFormValues function as a parameter to your xajax function, the PHP function will receive and array that looks like this:
array (
'checkbox' =>
array (
0 => 'check1',
1 => 'check2',
2 => 'check3',
),
)
The array argument to your function mirrors the structure that the $_GET array would have if you were to submit the form traditionally. You can then access the checkbox data in the array like this:
$aFormData['checkbox'][0]