一个用php实现ajax原来如此简单-xajax

来源:互联网 发布:河北人怎么样知乎 编辑:程序博客网 时间:2024/05/01 18:25
  上个学期自己用php做了个ajax无刷新聊天室拿去参加比赛拿了奖。那时ajax是新事物,也很少人知道,做出来的效果让人震撼。第一次写ajax代码感觉最深就是太繁琐,没有一定js功底的人会晕掉的!但凭着兴趣我还是埋头努力去写代码,当一个无刷新聊天室做好后,给我的感觉已经不是接受新技术的兴奋了,而是在想以后这技术真流行开来,那不累死程序员了!幸好我不是程序员,只是个爱好者。到了今天ajax果然展现了它的魅力,变得很火,非常流行,讨论异议也非常多的一个技术,ajax加入了很多大型动态语言的框架,连微软也做出相应的动作去支持ajax。我是用php的,我用php的编程工具不过就是EDITPLUS,没有asp.net的IDE那样智能,也不用一些所谓的php框架,想到要实现ajax的功能只能从键盘上一个一个代码的敲出来。反正我不是专业的编程高手,对这种情况很无奈,以后还怎么用AJAX
啊。最后发现xajax,专为php设计,是个好东西,只要几行代码就实现了ajax。象我这么懒的人,以后就用它了!
 
 
 
 
 
 
 
 
如何在我的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:
  1. 包含xajax类库:
以下是代码片段:

require_once("xajax.inc.php");
  1. 实例化xajax 对象:
以下是代码片段:

$xajax 
= new xajax();
  1. 注册你需要通过xajax调用的PHP函数的名称:
以下是代码片段:

$xajax
->registerFunction("myFunction");
  1. 编写注册的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(); 
}
  1. 在你的脚本输出任何信息之前,调用xajax用于接管请求:
以下是代码片段:

$xajax
->processRequests();
  1. 在页面的 <head></head> 标签之间, 告诉xajax生成所必需的JavaScript:
以下是代码片段:
<?php $xajax->printJavascript(); ?> 
  1. 在程序中,从JavaScript事件或者函数调用前面注册的函数:
以下是代码片段:

<div id="SomeElementId"></div
<
button onclick="xajax_myFunction(SomeArgument);">
  1. Include the xajax class library:
以下是代码片段:

require_once("xajax.inc.php");
  1. Instantiate the xajax object:
以下是代码片段:

$xajax 
= new xajax();
  1. Register the names of the PHP functions you want to be able to call through xajax:
以下是代码片段:

$xajax
->registerFunction("myFunction");
  1. Write the PHP functions you have registered and use the xajaxResponse object to return XML commands from them:
  2. function myFunction($arg)
  3. {
  4.   // do some stuff based on $arg like query data from a database and
  5.   // put it into a variable like $newContent
  6.  
  7.   // Instantiate the xajaxResponse object
  8.   $objResponse = new xajaxResponse();
  9.  
  10.   // add a command to the response to assign the innerHTML attribute of
  11.   // the element with id="SomeElementId" to whatever the new content is
  12.   $objResponse->addAssign("SomeElementId","innerHTML", $newContent);
  13.  
  14.   //return the XML response generated by the xajaxResponse object
  15.   return $objResponse->getXML();
}
  1. Before your script sends any output, have xajax handle any requests:
以下是代码片段:

$xajax
->processRequests();
  1. Between your <head></head> tags, tell xajax to generate the necessary JavaScript:
以下是代码片段:
<?php $xajax->printJavascript(); ?> 
  1. Call the function from a JavaScript event or function in your application:
  2. <div id="SomeElementId"></div>
<button onclick="xajax_myFunction(SomeArgument);">
 
就这么简单. 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.