源码之家:www.mycodes.net

PHP

如何在我的PHP脚本之中使用xajax

 如何在我的PHP脚本之中使用xajax?(get

下载链接: https://pan.baidu.com/s/1gfkY3mj 密码: bcvu

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.  

联系我们Contact

Copyright © 2013 - 2024 mycodes.net All rights reserved.

公司位置