譯者按: xAJax 最大的特點是他采用了XML response,這樣我們可以用php來布置,處理異步傳送數據之後,網頁內容的更新。而這些操作其它的ajax 框架都是由JS來完成的的。xAJax 使我們只需要寫一些PHP函數,就可以實現。
所有學好xajax的關健在於熟練掌握 xAJaxresponse 類。
tutorials:learn xAJax in 10 minutes
教程:十分鐘學會 xAJax
using xAJax in a PHP script
一個使用的xAJax的PHP腳本:
include the xAJax class library:
調用xAJax類庫:
require_once("xAJax.inc.PHP");
instantiate the xAJax object:
實例化xAJax對象
$xajax = new xAJax();
register the names of the PHP functions you want to be able to call through xAJax:
注冊一個你想用xAJax來調用的PHP函數名(與Javascript中的函數名相對應 xAJax_myfunction)
$xAJax->registerfunction("myfunction");
write the PHP functions you have registered and use the xAJaxresponse object to return XML commands from them:
編寫那個你剛剛已經注冊的PHP函數,並從中用 xAJaxresponse 對象來返回XML指令集
function myfunction($arg)
{
// do some stuff based on $arg like query data from a database and
// put it into a variable like $newcontent
//對參數$arg做一些諸如:從數據庫中獲取數據後定義給$newcontent 變量的基本操作
// instantiate the xAJaxresponse object
//實例化 xAJaxresponse 對象
$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
// 在響應實例中添加一個命令,用來將id為someelementid的innerHtml元素屬性
// 變為任何新的內容.
$objresponse->addassign("someelementid","innerHtml", $newcontent);
//return the XML response generated by the xAJaxresponse object
//返回由 xAJaxresponse 對象所生成的XML 響應
return $objresponse->getXML();
}
before your script sends any output, have xAJax handle any requests:
在你腳本傳送出任何東西前,xAJax都要處理所有請求
$xAJax->processrequests();
between your <head></head> tags, tell xAJax to generate the necessary Javascript:
在該頁的<head>和</head>標簽之間插入下列代碼,使xAJax實例可以自己生成所必需的JS
<?PHP $xAJax->printJavascript(); ?>
call the function from a Javascript event or function in your application:
從你程序中的JS 事件或函數調用之前你已經注冊過的相對應函數
<div id="someelementid"></div>
<button >
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.
只需這些步驟。其他的交由xAJax 去處理吧。你最主要的任務只是編寫PHP中的函數,只要使它們能返回xAJax的XML響應就行了,而這步可以用xAJaxresponse 類輕松解決。
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.
xajax最獨特的長處也許就是 xajaxresponse class了。其它的ajax庫需要你親自寫用JS寫回調的句柄,來處理一個異步請求而且得到的數據,並更新其內容。另一方面,xajax只需你簡單的控制好php的內容。然後通過xAJaxresponse 類,使在你的PHP函數中創建xml指令返回給你的程序。XML將被 xajax的信息(pump)解析。其指令告知xajax將如何更新內容和你程序中的位置。現在xAJaxresponse 已經提供了大量並有幫助的指令:http://www.flASPx.com/weblog/blog.PHP?bid=16 (略...付上詳細的xAJaxresponse 類說明)
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:
一個單獨xml響應可以包含多條命令,他們將依據加入響應的順序來被執行。舉個例子吧,讓我們假設一個用戶在你的程序中按下了一個按鈕。這個按下的事件將調用被JS封裝好的php函數。這個封包通過 XMLhttprequest 發出了一個異步請求給服務器,讓xAJax調用php函數。這個PHP函數做了一個查詢數據庫,一些數據處理或排序的操作。而你要用 xajaxresponse 類來產出一個 xAJax 的XML響應,它包含了多條命令。送給xAJax 信息pump來執行:
$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();
the xAJax message pump would parse the XML message and perform the following:
xAJax信息pump將會解析下列XML信息,並執行以下操作:
the value of the element with id myinput1 would be assigned to the data in $datafromdatabase.
將變量$datafromdatabase賦值給id為myinput1的value元素。
the color of the text in the element with id myinput1 would be changed to red.
id為myinput1的字體顏色元素將被換成紅色.
the data in $datafromdatabase2 would be appended to the innerHtml of the element with id mydiv1.
$datafromdatabase2,此數據將被追加到id為mydiv1的innerthml元素的結束部位
the data in $datafromdatabase3 would be prepended to the innerHtml of the element with id mydiv2.
$datafromdatabase3,此數據將被添加到id為mydiv2的innerthml元素的開始部位
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.
id為mydiv3的innerHtml元素中所有的 "xajax" 將被替換成 "xajax",使所有的xAJax以粗體顯示。
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.
會有一個輸入框彈出,並詢問用戶姓名。從輸入框取得的變量將轉換成JS變量並命名為x。
all of this is implemented on the server side in the PHP function by forming and returning an xAJax XML response.
所有這些組成了PHP函數在服務器端被執行,然後傳回一個XML響應。