xAjax简单使用范例

  xAjax是一个开源的基于PHP的Ajax解决方案,它使用简单,官方提供的帮助显示,只需要七步就可以了创建一个简单的ajax应用。

首先,在php文件头包含xajax类
[coolcode lang=”php”]
require_once(“xajax.inc.php”);
[/coolcode]
接下来,创建xajax对象
[coolcode lang=”php”]
$xajax = new xajax();
[/coolcode]
注册你的函数。这个函数用于在服务器端处理相应的客户端请求,并返回指定内容。
[coolcode lang=”php”]
$xajax->registerFunction(“myFunction”);
[/coolcode]
编写上述函数的代码。根据你要执行的功能
[coolcode lang=”php”]
function myFunction($arg)
{
// 在函数中使用从服务器或客户端得到的参数。
$newContent = $arg;

// 创建xajaxResponse对象。
// 这个是用于被客户端调用并返回相应内容的函数中所必须定义的。
$objResponse = new xajaxResponse();

// 使用xajaxResponse对象中的方法,执行不同的操作。
$objResponse->addAssign(“SomeElementId”,”innerHTML”, $newContent);

// 向客户端返回内容。
// 客户端的页面将在不刷新的情况下被重写。
return $objResponse->getXML();
}
[/coolcode]
打开请求控制器,所以请求都将交由xajax系统来处理。
[coolcode lang=”php”]
$xajax->processRequests();
[/coolcode]
在”head”标签中加入下列代码,用于在页面中生成必要的Javascript脚本。
[coolcode lang=”php”]
printJavascript(); ?>
[/coolcode]
最后,在页面中有事件发生时,调用你在服务器端定义的函数来进行处理就可以了。这里是名为myFunction的函数。注意,调用时,函数前必须加xajax_前缀。
[coolcode lang=”html”]

Category: 西拉东扯
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image