欢迎光临
我们一直在努力

十分钟学会 xajax

建站超值云服务器,限时71元/月

译者按:xajax 最大的特点是他采用了xml response,这样我们可以用php来布置,处理异步传送数据之后,网页内容的更新。而这些操作其它的ajax 框架都是由js来完成的的。xajax 使我们只需要写一些php函数,就可以实现。 Www_Chinaz_com

所有学好xajax的关健在于熟练掌握 xajaxresponse 类。 Chinaz.com

tutorials:learn xajax in 10 minutes

中国.站长站

教程:十分钟学会 xajax 中国站长_站,为中文网站提供动力

using xajax in a php script

Www_Chinaz_com

一个使用的xajax的php脚本:

中国站长.站

include the xajax class library: Chinaz@com

调用xajax类库: Www^Chinaz^com

require_once("xajax.inc.php");

Www_Chinaz_com

instantiate the xajax object: 中.国站长站

实例化xajax对象 中.国站长站

$xajax = new xajax();

站.长.站

register the names of the php functions you want to be able to call through xajax:

Www~Chinaz~com

注册一个你想用xajax来调用的php函数名(与javascript中的函数名相对应 xajax_myfunction) Www~Chinaz~com

$xajax->registerfunction("myfunction"); Chinaz.com

write the php functions you have registered and use the xajaxresponse object to return xml commands from them: 中国.站长站

编写那个你刚刚已经注册的php函数,并从中用 xajaxresponse 对象来返回xml指令集 Www@Chinaz@com

以下为引用的内容:

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:

Chinaz^com

Www.Chinaz.com

在你脚本传送出任何东西前,xajax都要处理所有请求 Www~Chinaz~com

$xajax->processrequests();

Www^Chinaz^com

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:

Chinaz^com

Www_Chinaz_com

从你程序中的js 事件或函数调用之前你已经注册过的相对应函数 Chinaz

以下为引用的内容:
<div id="someelementid"></div>
<button onclick="xajax_myfunction(someargument);">
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.
Chinaz.com

只需这些步骤。其他的交由xajax 去处理吧。你最主要的任务只是编写php中的函数,只要使它们能返回xajax的xml响应就行了,而这步可以用xajaxresponse 类轻松解决。

Www@Chinaz@com

how do i update my content asynchronously?

站.长.站

如何异步更新我的内容? Chinaz^com

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. Www^Chinaz^com

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来执行:

Chinaz_com

以下为引用的内容:

$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:

站长.站

Chinaz@com

xajax信息pump将会解析下列xml信息,并执行以下操作: Chinaz^com

the value of the element with id myinput1 would be assigned to the data in $datafromdatabase.

Chinaz

将变量$datafromdatabase赋值给id为myinput1的value元素。

中国.站.长站

the color of the text in the element with id myinput1 would be changed to red. Www_Chinaz_com

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. Www~Chinaz~com

$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. Www.Chinaz.com

id为mydiv3的innerhtml元素中所有的 "xajax" 将被替换成 "xajax",使所有的xajax以粗体显示。 Chinaz@com

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. Www_Chinaz_com

会有一个输入框弹出,并询问用户姓名。从输入框取得的变量将转换成js变量并命名为x。

中.国.站.长.站

all of this is implemented on the server side in the php function by forming and returning an xajax xml response. Www@Chinaz@com

所有这些组成了php函数在服务器端被执行,然后传回一个xml响应。

中国站.长站

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 十分钟学会 xajax
分享到: 更多 (0)