??xml version="1.0" encoding="utf-8" standalone="yes"?>
1Qhelloworld
helloworld是每一个编E语a所必须的,所以这里也不例外,我会(x)׃码给?br />
<?php
// helloworld.php demonstrates a very basic xajax implementation
// using xajax version 0.1 beta4
// http://xajax.sourceforge.n...
require ('xajax.inc.php'); #必须的,攑ֈ脚本的最前面
#调用函数Q该函数被javascript调用
function helloWorld($isCaps)
{
if ($isCaps)
$text = "HELLO WORLD!";
else
$text = "Hello World!";
$objResponse = new xajaxResponse();
$objResponse->addAssign("div1","innerHTML",$text); #lid为div1的html元素的innerHTML属性分?text指,其他的方法请参考前面脓(chung)出的XAJAX介绍
return $objResponse->getXML(); #q回赋值后的代码,采用xml格式
}
// Instantiate the xajax object. No parameters defaults requestURI to this page, method to POST, and debug to off
$xajax = new xajax();
//$xajax->debugOn(); // Uncomment this line to turn debugging on
// Specify the PHP functions to wrap. The JavaScript wrappers will be named xajax_functionname
$xajax->registerFunction("helloWorld"); #注册函数Q这个也是必ȝQ用来保证,javascript能调用php里面的函?br />// Process any requests. Because our requestURI is the same as our html page,
// this must be called before any headers or HTML output have been sent
$xajax->processRequests();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm....
<html>
<head>
<title>xajax example</title>
<?php
#输出javascript脚本Q这个方法必L在head之间Q特别是应该攑֜<html>标签之前Q否则无法工l?br />$xajax->printJavascript();
?>
</head>
<body style="text-align:center;">
<div id="div1" name="div1"> </div>
<br/>
<button onclick="xajax_helloWorld(0)" >Click Me</button>
<button onclick="xajax_helloWorld(1)" >CLICK ME</button>
<script type="text/javascript">
xajax_helloWorld(0); // call the helloWorld function to populate the div on load
</script>
</body>
</html>
上面的脚本是一个简单的例子Q但是体CXAJAX使用的一个流E。需要注意的是在html脚本中,我们调用的函数名?br />xajax_helloWorld
(),但是我们~程和注册的函数名却是helloWorldQ这不是一个错误,而是必须q样做,你可以查看xajax.inc.php的代码就知道Q他?
认的在html调用的函数名前缀是xajax_helloWorld()Q因此如果你不修改xajax.inc.phpQ那么你在html里面调用函数
必d上xajax_q样的前~?br />
E序演示
2Q表单处?br />q个例子是我花时间比较多的,也是大家比较兛_的,虽然官方也给Z一个form的例子,但是对表单数据的处理都是自己再重新写的函敎ͼ而没有用xajax.getFormValues函数Q个得比较复杂。下面是官方的form演示和源代码
form演示
form源代?/a>
下面是我的代?br />
<?php
require("includes/xajax.inc.php");
function doSomething( $formData )
{
$objResponse = new xajaxResponse();
$str="";
foreach($formData as $key=>$value)
if (empty($str))
$str.=$key."='".$value."'";
else
$str.=",".$key."='".$value."'";
//$str="form data is ".implode(",",$formData);
//$objResponse->addAlert(print_r($formData));
$objResponse->addAssign("result", "innerHTML", $str );
return $objResponse->getXML();
}
function hb()
{
$objResponse = new xajaxResponse();
$objResponse->addAssign("doIt", "value", "Working...");
$objResponse->addAssign("doIt", "disabled", true);
$objResponse->addScript("xajax_doSomething(xajax.getFormValues('someForm'));");
return $objResponse->getXML();
}
?>
<?php
$xajax = new xajax();
//$xajax->debugOn();
$xajax->registerFunction("doSomething");
$xajax->registerFunction("hb");
$xajax->processRequests();
?>
<HTML>
<HEAD>
<?php $xajax->printJavascript(); ?>
</HEAD>
<BODY>
<form id='someForm'>
<table>
<tr>
<td>username:
<input type="text" name='user' id='user' />
</td>
<td>password:<input type="password" id='pwd' name='pwd' /></td>
<td><input type='button' id='doIt' value='Do It' onClick="xajax_hb()"></input>
</td></tr>
</table>
</form>
<div id='result'>
</div>
</BODY>
</HTML>
q个要注意的比较?br />aQ表单的元素必须有name属性,q个一定要注意Q这是我痛苦了一天得到的l果Q否则getFormValues函数得不到表单数据?br />b) doSomething($formData)函数中的参数$formData是一个数l元素,索引是表单元素的名称Q属性name的|Q其值是表单元素的|属性value的|
cQ?如果debugon打开了,可能?x)出现死循环的警告窗口,目前q不知道原因?/font>
]]>
转自Qhttp://spaces.msn.com/blackwoodk ... 2wpZwYtrg!169.entry
最q研I了一下ajaxQ最后选了xajax作ؓ(f)基本class
参考站点:(x)http://xajax.sourceforge.net
因ؓ(f)基本都是gb2312所以把xajax.inc.php里面的编码改了一下,不过老实说UTF-8不错Q?
$this->xml = "<?xml version="1.0" encoding="GB2312"?>";
重新装了一下KaneXajaxc,
除此以外基本没怎么改动?
<?php
require_once(‘Smarty/Smarty.class.php?;
require_once(‘Xajax/xajax.inc.php?;
function myFunction($arg)
{
$objResponse = new xajaxResponse();
$objResponse->addAssign("SomeElementId","innerHTML", $newContent);
return $objResponse->getXML();
}
$Kanexajax = new KaneXajax();
$Kanexajax->registerFunction("myFunction");
$Kanexajax->processRequests();
$KaneSMARTY = new KaneSmarty();
$KaneSMARTY->assign(‘xajax_javascript? $xajax->getJavascript());
$KaneSMARTY->display(myTEMPLATE);
?>
一个最基本的phpok了,当然tpl里面<{$xajax_javascript}>要加?lt;HEAD></HEAD>之间
再说说基本的Ҏ(gu)吧:(x)
* addAssign($sTargetId,$sAttribute,$sData)
讄$sTargetId元素?sAttribute属性ؓ(f)$sDataQ常用的有div的innerHTMLQinput的value什么的?
$objResponse->addAssign("contentDiv","innerHTML","Some Text");
$objResponse->addAssign("checkBox1","checked","true");
* addAppend($sTargetId,$sAttribute,$sData)
q加$sData?sTargetId元素?sAttribute属性上
$objResponse->addAppend("contentDiv","innerHTML","Some Text");
* addPrepend($sTargetId,$sAttribute,$sData)
预制$sTargetId元素?sAttribute属性ؓ(f)$sDataQ老实_(d)没在实战中用q呢Q呵?
$objResponse->addPrepend("contentDiv","innerHTML","Some Text");
* addReplace($sTargetId,$sAttribute,$sSearch,$sData)
替换$sTargetId元素?sAttribute属性中?sSearch?sData?
$objResponse->addReplace("contentDiv","innerHTML","text","<strong>text</strong>");
* addClear($sTargetId,$sAttribute)
清空$sTargetId元素?sAttribute属?
$objResponse->addClear("Input1","value");
* addCreate($sParentId, $sTagname, $sId, $sType)
创徏$sParentId元素的一个新的子元素Q其name?sTagnameQId?sIdQ类型ؓ(f)$sType
$objResponse->addCreate("form1","input", "pass", "password");
* addRemove($sElementId)
U除$sElementId元素
$objResponse->addRemove("div1");
* addAlert($sMsg)
弹出一个内容ؓ(f)$sMsg的提C框
$objResponse->addAlert("This is some text");
* addScript($sJS)
执行一Djavascript
$objResponse->addScript("var txt = prompt(‘get some text?;");
$objResponse->addScript("window.location=""."demo.php?id=1"."";");
q两天用PHP+SMARTY+XAJAX写了一些小东西Q感觉上是调试完全靠感觉,不过
按照DB的debugQSmarty的debugQxajax的debugOn()Q的步骤Q一步步地测好再往下写?x)好很?
因ؓ(f)Qsmarty已经够敏感,l常l你一张大白脸Q而xajax更加敏感QQ何的地方有一点小错误Q他立刻完蛋Q这L(fng)女h娶回家真是挺可怕的Q不q谁让她支持Smarty呢?
q两天再把改好的一些代码脓(chung)上来吧,感觉q有的优?/div>
]]>
tutorials:learn xajax in 10 minutes
教程:十分钟学?xajax
using xajax in a php script
一个用的xajax的php脚本:
include the xajax class library:
调用xajaxcd:
instantiate the xajax object:
实例化xajax对象
register the names of the php functions you want to be able to call through xajax:
注册一个你想用xajax来调用的php函数?与javascript中的函数名相对应 xajax_myfunction)
write the php functions you have registered and use the xajaxresponse object to return xml commands from them:
~写那个你刚刚已l注册的php函数Qƈ从中?xajaxresponse 对象来返回xml指o(h)?/p>
before your script sends any output, have xajax handle any requests:
在你脚本传送出M东西?xajax都要处理所有请?/p>
between your <head></head> tags, tell xajax to generate the necessary javascript:
在该늚<head>?lt;/head>标签之间插入下列代码Qxajax实例可以自己生成所必需的js
call the function from a javascript event or function in your application:
从你E序中的js 事g或函数调用之前你已经注册q的相对应函?/p>
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.
只需q些步骤。其他的交由xajax d理吧。你最主要的Q务只是编写php中的函数Q只要它们能返回xajax的xml响应p了,而这步可以用xajaxresponse c轻松解冟?/p>
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 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.
xajax最独特的长处也许就?xajaxresponse class了。其它的ajax库需要你亲自写用js写回调的句柄Q来处理一个异步请求而且得到的数据,q更新其内容。另一斚wQxajax只需你简单的控制好php的内宏V然后通过xajaxresponse c,使在你的php函数中创建xml指o(h)q回l你的程序。xml被 xajax的信?pump)解析。其指o(h)告知xajax如何更新内容和你程序中的位|。现在xajaxresponse 已经提供了大量ƈ有帮助的指o(h)Q?a target="_blank">http://www.flaspx.com/weblog/blog.php?bid=16 (?..付上详细的xajaxresponse c说?
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响应可以包含多条命o(h)Q他们将依据加入响应的顺序来被执行。D个例子吧Q让我们假设一个用户在你的E序中按下了一个按钮。这个按下的事g调用被js装好的php函数。这个封包通过 xmlhttprequest 发出了一个异步请求给服务器,让xajax调用php函数。这个php函数做了一个查询数据库Q一些数据处理或排序的操作。而你要用 xajaxresponse cL产出一?xajax 的xml响应Q它包含了多条命令。送给xajax 信息pump来执?
the xajax message pump would parse the xml message and perform the following:
xajax信息pump会(x)解析下列xml信息Qƈ执行以下操作:
the value of the element with id myinput1 would be assigned to the data in $datafromdatabase.
变?datafromdatabase赋值给id为myinput1的value元素?/p>
the color of the text in the element with id myinput1 would be changed to red.
id为myinput1的字体颜色元素将被换成红?
the data in $datafromdatabase2 would be appended to the innerhtml of the element with id mydiv1.
$datafromdatabase2,此数据将被追加到id为mydiv1的innerthml元素的结束部?/p>
the data in $datafromdatabase3 would be prepended to the innerhtml of the element with id mydiv2.
$datafromdatabase3,此数据将被添加到id为mydiv2的innerthml元素的开始部?/p>
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.
id为mydiv3的innerhtml元素中所有的 "xajax" 被替换?"xajax",使所有的xajax以粗体显C?/p>
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.
?x)有一个输入框弹出Qƈ询问用户姓名。从输入框取得的变量{换成js变量q命名ؓ(f)x?br />all of this is implemented on the server side in the php function by forming and returning an xajax xml response.
所有这些组成了php函数在服务器端被执行Q然后传回一个xml响应?/p>
1、用xmlhttprequest从服务器获得xml数据放于id="xml"的div?/p>
也可以从各种装的xmlhttprequest框架中直接获得xmlQ这U方式更好一?/p>
2、对应xsl攄于id="xslt"的div?/p>
3、通过Ajaxsltl合成htmlQ结果将昄于id="htmldisplay"的div?/p>
加蝲如下js
<script src="../misc.js" type="text/javascript"></script> <script src="../dom.js" type="text/javascript"></script> <script src="../xpath.js" type="text/javascript"></script> <script src="../xslt.js" type="text/javascript"></script>
然后
var xml = xmlParse(el('xml').value); var xslt = xmlParse(el('xslt').value); var html = xsltProcess(xml, xslt); document.getElementById('html').value = html; document.getElementById('htmldisplay').innerHTML = html;
很简单就完成了xml到html的{化,而xpath/xsl的强大功能得我们可以非常更斚w的处理xml数据
备注:
misc.jsQ一些常量定义和 helper 函数Q还?log 的实现?/p>
dom.jsQXML DOM 接口?JS 实现Q主要的函数?xmlParse( XMLString )Q调用成功后可以用标准 DOM 方式来操控返回的 XDocument 了?/p>
xpath.jsQXPath ?JS 实现?/p>
xslt.jsQXSLT ?JS 实现Q要用到里面?xsltProcess( XML, XSLT )Q给?XML ?XSLT 得到转换的结果?/p>