首页 > 开发 > PHP > 正文

Xajax手册(第一版)

2024-05-04 22:58:44
字体:
来源:转载
供稿:网友

原文:http://xajax.sourceforge.net/
英文原版:copyright © 2005 j. max wilson
简体中文翻译:honestqiao(乔楚)/2005-12-7 17:23/(第一版)
什么是xajax?
xajax如何工作?
为什么我要使用xajax代替其他php的ajax库?
如何在我的php脚本之中使用xajax?
如何异步更新内容?
如何异步处理表单数据?
如何给xajax增加定制功能?
我能在私有或者收费产品之中使用xajax吗?
 
what is xajax?
how does xajax work?
why should i use xajax instead of another ajax library for php?
how do i use xajax in my php script?
how do i update my content asynchronously?
how do i process form data asynchronously?
how do i add custom functionality to xajax?
may i use xajax in a proprietary product and charge for it?
什么是xajax?
xajax是一个开源的 php 类库 它能够让你黏合html、css、javascript和php,轻而易举的开发功能强大、基于web的ajax应用软件. 使用xajax开发的应用软件,无需重新调入页面,就能够异步调用服务器端的php函数和更新内容.
what is xajax?
xajax is an open source php class library that allows you to easily create powerful, web-based, ajax applications using html, css, javascript, and php. applications developed with xajax can asynchronously call server-side php functions and update content without reloading the page.
 
xajax 如何工作?
你的应用软件需要异步调用的php函数, xajax的php对象都生成了对应的封装好了的javascript函数. 当被调用时,封装的函数使用javascript的xmlhttprequest对象与服务器异步通讯,调用xajax对象对应的php函数. 调用结束后, php函数由xajax返回一个xajax的xml响应传递给应用程序. xml响应包含了特定的指令和数据,他们可以被xajax的javascript消息分析器解析,并且被用于更新你的应用程序的内容.
how does xajax work?
the xajax php object generates javascript wrapper functions for the php functions you want to be able to call asynchronously from your application. when called, these wrapper functions use javascript's xmlhttprequest object to asynchronously communicate with the xajax object on the server which calls the corresponding php functions. upon completion, an xajax xml response is returned from the php functions, which xajax passes back to the application. the xml response contains instructions and data that are parsed by xajax's javascript message pump and used to update the content of your application.
为什么我要使用xajax代替其他php的ajax库?
你应该选择一个最是和你的项目需要的库.
xajax 提供了以下的功能, 它们使得ajax富有特色而又功能强大:
why should i use xajax instead of another ajax library for php?
you should choose whatever library will best meet the needs of your project.
xajax offers the following features that, together, make it unique and powerful:
xajax特殊的 xml 响应 / javascript 消息分析系统 帮助你做到, 自动的处理函数返回的数据,按照php函数返回的指令更新内容或者状态. 因为xajax作了这些工作because xajax does the work, 你不需要写javascript的回调处理函数.
xajax's unique xml response / javascript message-pump system does the work for you, automatically handling the data returned from your functions and updating your content or state according to the instructions you return from your php functions. because xajax does the work, you don't have to write javascript callback handler functions.
xajax反对将代码和数据紧密地杂糅在一起的主张, 并且保持xajax的代码从与他代码分离. 因为它是对象构造的, 你可以加上自己定制的功能给xajax去扩展xajaxresponse 类和使用addscript方法.
xajax is object oriented to maintain tighter relationships between the code and data, and to keep the xajax code separate from other code. because it is object oriented, you can add your own custom functionality to xajax by extending the xajaxresponse class and using the addscript() method.
xajax 可以工作在 firefox, mozilla, 大部分基于 mozilla 的浏览器, internet explorer, 和 safari.
xajax works in firefox, mozilla, probably other mozilla based browsers, internet explorer, and safari.
除了更新元素的值和内含的html内容(innerhtml), xajax 还能用于更新样式(styles), css 类, 多选和单选按钮选择,甚至可以更新任何元素的属性.
in addition to updating element values and innerhtml, xajax can be used to update styles, css classes, checkbox and radio button selection, or nearly any other element attribute.
xajax 支持使用一维或者多维数组、关联数组(哈希数组) 作为xajax函数的参数从javascript传送给php. 反之additionally, 如果你传送一个javascript的对象给xajax函数,php函数将接受一个描叙对象属性的关联数组(哈希数组).
xajax supports passing single and multidimensional arrays and associative arrays from javascript to php as parameters to your xajax functions. additionally, if you pass a javascript object into an xajax function, the php function will receive an associative array representing the properties of the object.
xajax 提供了一种简单的异步表单处理方式. 使用 xajax.getformvalues() javascript 方法, 你可以轻而易举的在表单之中提交一个描绘值的数组作为参数传送到xajax异步处理函数:
xajax_processform(xajax.getformvalues('formid');
. 它可以处理复杂的input 元素名称 ,例如 "checkbox[][]" 或者 "name[first]" 产生的多维或者关联数组(哈希数组), 就是普通提交表单那样使用php的$_get数组
xajax provides easy asynchronous form processing. by using the xajax.getformvalues() javascript method, you can easily submit an array representing the values in a form as a parameter to a xajax asynchronous function:
xajax_processform(xajax.getformvalues('formid');
. it even works with complex input names like "checkbox[][]" and "name[first]" to produce multidimensional and associative arrays, just as if you had submitted the form and used the php $_get array
使用xajax你可以动态的发送附加的javascript作为请求的响应到你的应用软件中运行,而这就和动态的更新元素的属性一样方便.
using xajax you can dynamically send additional javascript to your application to be run in response to a request as well as dynamically update element attributes.
xajax会自动比较php函数返回的数据与你已经标记需要修改的元素属性. 只有当新的数据确实可以改变现有的属性,属性才会真的被更新. 这将可消除程序在一定时间间隔内更新与当前内容相同或者不同的内容而出现的明显的闪烁.
xajax automatically compares the data returned from the php functions to the data that is already in the attribute of the element you have marked for change. the attribute is only updated with the new data if it will actually change what is already in the attribute. this eliminates the flicker often observed in applications that update content at a regular time interval with data which may or may not differ from extant content.
每一个通过xajax简单注册的函数都可以有不同的请求方式. 所有的函数默认使用post方式,除非你明确的指定使用get方式. 如果使用get请求,你必须非常明确的考虑它确实是你所需要的
each function registered to be accessible through xajax can have a different request type. all functions default to use post unless get is explicitly set. this is to encourage careful consideration of when to use get requests
如果没有指定uri,xajax将设法自动确定脚本的uri. xajax的自动确定算法经过了充分的验证,确保在大部分https或者未知端口的http服务器上也可以正常的工作.
if no request uri is specified, xajax tries to autodetect the url of the script. the xajax autodetection algorithm is sophisticated enough that, on most servers, it will work under a secure https:// protocol as well as http:// and with nonstandard ports.
xajax使用utf8编码所有的请求和响应,以确保支持绝大部分的字符和语言. xajax已经成功测试了多种不同的unicode字符,包括spanish, russian, arabic, and hebrew
xajax encodes all of its requests and responses in utf-8 so that it can support a wider range of characters and languages. xajax has been successfully tested with various unicode characters including spanish, russian, arabic, and hebrew
几乎所有xajax生成的javascript都可以通过动态的外部javascript包含到你的web程序之中. 当你在浏览器之中查看程序的源代码时,不会有javascript的函数定义混杂在html标记之中.
nearly all of the javascript generated by xajax is included into your web application through dynamic external javascript. when you view the source of your application in your browser, the markup will be not cluttered by javascript function definitions.
xajax可以使用在smarty模板系统之中,仅仅需要创建一个smarty变量就可以包含xajax的javascript:
$smarty->assign('xajax_javascript', $xajax->getjavascript());
然后你可以使用在header模版之中使用
{$xajax_javascript}
从而把xajax应用到你的站点.
xajax can be used with the smarty templating system by creating a variable in smarty that contains the xajax javascript:
$smarty->assign('xajax_javascript', $xajax->getjavascript());
then you can use
{$xajax_javascript}
in your header template to use xajax on your site.
如何在我的php脚本之中使用xajax?
xajax的设计是如此的富有特色,以至于不管是已有的web程序还是新的项目,它都能够被极其简单的部署和应用. 仅仅需要七步,你就可以在几乎任何php脚本之中加入xajax的强大功能:
how do i use xajax in my php script?
xajax is designed to be extremely easy to implement in both existing web applications as well as new projects. you can add the power of xajax to nearly any php script in seven easy steps:
包含xajax类库:
require_once("xajax.inc.php");
实例化xajax 对象:
$xajax = new xajax();
注册你需要通过xajax调用的php函数的名称:
$xajax->registerfunction("myfunction");
编写注册的php函数,并且在函数之中使用xajaxresponse 对象返回xml指令:
function myfunction($arg)
{
   // 对$arg做一些基本处理例如从数据库检索数据
   // 然后把结果赋值给变量,例如$newcontent
  
   // 实例化xajaxresponse 对象
   $objresponse = new xajaxresponse();
  
   // 添加指令到响应之中,用于指派
   //指定元素(例如id="someelementid")的innerhtml属性的新的内容
   $objresponse->addassign("someelementid","innerhtml", $newcontent);
  
   //返回xajaxresponse 对象生成的xml响应
   return $objresponse->getxml();
}
在你的脚本输出任何信息之前,调用xajax用于接管请求:
$xajax->processrequests();
在页面的 <head></head> 标签之间, 告诉xajax生成所必需的javascript:
<?php $xajax->printjavascript(); ?>
在程序中,从javascript事件或者函数调用前面注册的函数:
<div id="someelementid"></div>
<button >
include the xajax class library:
require_once("xajax.inc.php");
instantiate the xajax object:
$xajax = new xajax();
register the names of the php functions you want to be able to call through xajax:
$xajax->registerfunction("myfunction");
write the php functions you have registered and use the xajaxresponse object to return xml commands from them:
function myfunction($arg)
{
  // do some stuff based on $arg like query data from a database and
  // put it into a variable like $newcontent

  // instantiate the 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
  $objresponse->addassign("someelementid","innerhtml", $newcontent);

  //return the xml response generated by the xajaxresponse object
  return $objresponse->getxml();
}
before your script sends any output, have xajax handle any requests:
$xajax->processrequests();
between your <head></head> tags, tell xajax to generate the necessary javascript:
<?php $xajax->printjavascript(); ?>
call the function from a javascript event or function in your application:
<div id="someelementid"></div>
<button >
 
就这么简单. xajax 会处理其他所有的事情. 你所要做的主要工作就是编写php函数,然后从函数之中返回xajax的xml响应。而后者通过xajaxresponse类可以非常简单的生成.
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最富有特色的功能或许就是xajaxresponse类. 其他的ajax库需要你自己编写javascript的回调句柄去处理一个异步请求返回的数据并更新内容. xajax, 从另外一个角度来说, 允许你使用php简单的控制内容. xajaxresponse 让你在php函数之中创建xml指令返回给你的程序. xml将被xajax的消息分析器解析,指令将告诉xajax如何更新程序的内容和状态. 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 the following commands:
addassign($stargetid,$sattribute,$sdata)
给命名为$stargetid的元素的$sattribute属性赋值$sdata
$objresponse->addassign("contentdiv","innerhtml","some text");
$objresponse->addassign("checkbox1","checked","true");
addassign($stargetid,$sattribute,$sdata)
assigns the $sattribute of the element identified by $stargetid to $sdata
$objresponse->addassign("contentdiv","innerhtml","some text");
$objresponse->addassign("checkbox1","checked","true");
addappend($stargetid,$sattribute,$sdata)
给命名为$stargetid的元素的$sattribute属性追加值$sdata
$objresponse->addappend("contentdiv","innerhtml","some text");
addappend($stargetid,$sattribute,$sdata)
appends $sdata to the $sattribute of the element identified by $stargetid
$objresponse->addappend("contentdiv","innerhtml","some text");
addprepend($stargetid,$sattribute,$sdata)
预备给命名为$stargetid的元素的$sattribute属性赋值$sdata
$objresponse->addprepend("contentdiv","innerhtml","some text");
addprepend($stargetid,$sattribute,$sdata)
prepends $sdata to the $sattribute of the element identified by $stargetid
$objresponse->addprepend("contentdiv","innerhtml","some text");
addreplace($stargetid,$sattribute,$ssearch,$sdata)
替换命名为$stargetid的元素的$sattribute属性的值之中的$ssearch为$sdata
$objresponse->addreplace("contentdiv","innerhtml","text","<strong>text</strong>");
addreplace($stargetid,$sattribute,$ssearch,$sdata)
replaces all instances of $ssearch with $sdata in the $sattribute of the element identified by $stargetid
$objresponse->addreplace("contentdiv","innerhtml","text","<strong>text</strong>");
addclear($stargetid,$sattribute)
清空命名为$stargetid的元素的$sattribute属性的值
$objresponse->addclear("input1","value");
addclear($stargetid,$sattribute)
clears the $sattribute of the element identified by $stargetid
$objresponse->addclear("input1","value");
addcreate($sparentid, $stagname, $sid, $stype)
在已经存在的命名为$sparentid的元素下添加一个名叫$stagname的子元素,并且把他的id赋值为$sid,可选的类型赋值为 $stype.
$objresponse->addcreate("form1","input", "pass", "password");
addcreate($sparentid, $stagname, $sid, $stype)
adds a new $stagname child element to an existing element identified by $sparentid, and assigns it the id $sid and the optional type $stype.
$objresponse->addcreate("form1","input", "pass", "password");
addremove($selementid)
从你的程序之中移除命名为$selementid的元素
$objresponse->addremove("div1");
addremove($selementid)
removes the element identified by $selementid from your application
$objresponse->addremove("div1");
addalert($smsg)
显示一个内容为 $smsg 的警告框(javascript的alert)
$objresponse->addalert("this is some text");
addalert($smsg)
display an alert box with $smsg
$objresponse->addalert("this is some text");
addscript($sjs)
执行javascript代码 $sjs (演示代码原文有误)
$objresponse->addscript("var txt = prompt('get some text');");
addscript($sjs)
execute the javascript code $sjs
$objresponse->addalert("var txt = prompt('get some text');");
 
一个独立的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]
如何给xajax增加定制功能?
xajax可以使用各种服加的用户定制功能进行扩展. 正因为xajax是完全面向对象的,并且可以使用xajaxresponse的addscript()方法,所以他具有无限扩展的可能. 你可以创建你自己的xajax响应类,来继承xajaxresponse 类以及它的方法,并加上你自己定制的响应. 让我们用一个定制的增加选择组合框(select combo boxes)选项的响应指令的例子来说明. 你可以象下面这样扩展xajaxresponse 类:
class myxajaxresponse extends xajaxresponse

  function addaddoption($sselectid, $soptiontext, $soptionvalue) 
  { 
    $sscript  = "var objoption = new option('".$soptiontext."','".$soptionvalue."');";
    $sscript .= "document.getelementbyid('".$sselectid."').options.add(objoption);";
    $this->addscript($sscript);
  }
}
现在, 取代xajaxresponse 对象的初始化, 把你自己的 myxajaxresponse 对象的初始化定义到你的 xajax php 函数之中:
$objresponse = new myxajaxresponse();
$objresponse->addassign("div1", "innerhtml", "some text"); 
$objresponse->addaddoption("select1","new option","13"); 
 
return $objresponse->getxml();
被调用时,这个方法将会发送需要的javascript到页面并执行. 当然你也有另外一种做法alternatively, 你可以在你的程序之中创建一个如下的javascript函数:
<script type="text/javascript">
function addoption(selectid,txt,val)
{
        var objoption = new option(txt,val);
        document.getelementbyid(selectid).options.add(objoption);
}
</script>
并且使用addscript() 调用这个方法:
$objresponse->addscript("addoption('select1','new option','13');");
how do i add custom functionality to xajax?
xajax can be extended with all kinds of additional custom functionality. the extendability of xajax is made possible because it is object oriented, and by the addscript() method of the xajaxresponse class. you can create your own xajax response class that extends the xajaxresponse class and has all of the normal xajaxresponse methods, plus your own custom responses. for instance, let's say that you wanted to add a custom response command to add options to select combo boxes. you could extend the xajaxresponse class like this:
class myxajaxresponse extends xajaxresponse

  function addaddoption($sselectid, $soptiontext, $soptionvalue) 
  { 
    $sscript  = "var objoption = new option('".$soptiontext."','".$soptionvalue."');";
    $sscript .= "document.getelementbyid('".$sselectid."').options.add(objoption);";
    $this->addscript($sscript);
  }
}
now, instead of instantiating an xajaxresponse object, you instantiate and use your myxajaxresponse object in your xajax php functions:
$objresponse = new myxajaxresponse();
$objresponse->addassign("div1", "innerhtml", "some text"); 
$objresponse->addaddoption("select1","new option","13"); 
 
return $objresponse->getxml();
this method would send the necessary javascript to the page when it was called and execute it. alternatively, you could create a javascript function in your application:
<script type="text/javascript">
function addoption(selectid,txt,val)
{
        var objoption = new option(txt,val);
        document.getelementbyid(selectid).options.add(objoption);
}
</script>
and call it with the addscript() method:
$objresponse->addscript("addoption('select1','new option','13');");
我能在私有或者收费产品之中使用xajax吗?
简而言之: 能,只要你愿意.
xajax php 类库的发布遵循 gnu lesser general public license (lgpl).
may i use xajax in a proprietary product and charge for it?
in short: yes, you may.
the xajax php class library is released under the gnu lesser general public license (lgpl).
 
简体中文手册版权所有 © 2005 honestqiao(乔楚)
copyright © 2005 j. max wilson

最大的网站源码资源下载站,

上一篇:获取客户端IP

下一篇:ADODB官方介绍

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表