learn xajax in 10 minutes(原文摘自xajaxproject 官方wiki)
譯者按: xajax 最大的特點(diǎn)是他采用了xml response,這樣我們可以用php來(lái)布置,處理異步傳送數(shù)據(jù)之后,網(wǎng)頁(yè)內(nèi)容的更新。而這些操作其它的ajax 框架都是由js來(lái)完成的的。xajax 使我們只需要寫一些php函數(shù),就可以實(shí)現(xiàn)。 所有學(xué)好xajax的關(guān)健在于熟練掌握 xajaxresponse 類。
tutorials:learn xajax in 10 minutes 教程:十分鐘學(xué)會(huì) xajax
using xajax in a php script 一個(gè)使用的xajax的php腳本:
include the xajax class library: 調(diào)用xajax類庫(kù):
require_once("xajax.inc.php");
instantiate the xajax object: 實(shí)例化xajax對(duì)象
$xajax = new xajax();
register the names of the php functions you want to be able to call through xajax: 注冊(cè)一個(gè)你想用xajax來(lái)調(diào)用的php函數(shù)名(與javascript中的函數(shù)名相對(duì)應(yīng) xajax_myfunction)
$xajax->registerfunction("myfunction");
write the php functions you have registered and use the xajaxresponse object to return xml commands from them: 編寫那個(gè)你剛剛已經(jīng)注冊(cè)的php函數(shù),并從中用 xajaxresponse 對(duì)象來(lái)返回xml指令集
function myfunction($arg) { // do some stuff based on $arg like query data from a database and // put it into a variable like $newcontent //對(duì)參數(shù)$arg做一些諸如:從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)后定義給$newcontent 變量的基本操作 // instantiate the xajaxresponse object //實(shí)例化 xajaxresponse 對(duì)象 $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 // 在響應(yīng)實(shí)例中添加一個(gè)命令,用來(lái)將id為someelementid的innerhtml元素屬性 // 變?yōu)槿魏涡碌膬?nèi)容. $objresponse->addassign("someelementid","innerhtml", $newcontent); //return the xml response generated by the xajaxresponse object //返回由 xajaxresponse 對(duì)象所生成的xml 響應(yīng) return $objresponse->getxml(); }
before your script sends any output, have xajax handle any requests: 在你腳本傳送出任何東西前,xajax都要處理所有請(qǐng)求
$xajax->processrequests();
between your <head></head> tags, tell xajax to generate the necessary javascript: 在該頁(yè)的<head>和</head>標(biāo)簽之間插入下列代碼,使xajax實(shí)例可以自己生成所必需的js
<?php $xajax->printjavascript(); ?>
出處:藍(lán)色理想
責(zé)任編輯:moby
上一頁(yè) 下一頁(yè) 十分鐘學(xué)會(huì) xajax [2]
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|