一、xajax与其它ajax框架的比较
xajax功能很简单,但很灵活!~它不象其它一些大的框架,功能确实强大,但执行速度不敢恭维。。功能虽多,但不够灵活。api多,学起来简直如同学习一门新的语言。
二、xajax功能介绍
xajax的功能比较简单,但就因为简单,所以灵活。同时,这也要求使用者要对javascrīpt / vbs 客户端脚本有一定的了解。因为它的功能是比较活的。可以说单纯使用xajax,什么也做不出,但配合js / vbs 又什么都做的出。
xajax主要是使用xajaxResponse这个类,它提供了一些方法,举例如下:
1、addAlert($sMsg)
弹出警告
2、addscrīpt($sJS)
执行某段js
3、$objResponse->addAssign("","","")
给页面中的某个元素附值,或者修改其属性
等等。。。。
所以xajax不是死的,它不能做出XXX XXX功能来,但是它可以灵活的控制客户端的js / vbs,去完成我们要实现的效果。
三、xajax安装配置
不需要特别的安装和配置,只要下载其文件包,解压到网站目录中
下载地址:
http://www.xajaxproject.org/
三、用xajax做会员注册和登陆
1、数据库
采用mysql5.0,数据库名zl 表名zl_user 表结构
id int(11) auto_increment
zl_user varchar(50)
zl_pwd varchar(50)
email varchar(50)
http://blog.knowsky.com/
2、reg.php 注册文件(内有说明)
<?php
require_once("inc/xajax.inc.php");
//使用xajax则必须首先引入xajax.inc.php
$xajax = new xajax("inc/signup.php");
//创建一个xajax对象,为singup.php
$xajax->registerFunction("processForm");
//使用singup.php里的processForm函数
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" href="http://blogbeta.blueidea.com/css/style.css" _fcksavedurl=""http://blogbeta.blueidea.com/css/style.css"" type="text/css">
<title>无标题文档</title>
<?php $xajax->printJavascrīpt('inc/'); ?>
<scrīpt type="text/javascrīpt">
function submitSignup()
{
xajax.$('submitButton').disabled=true;
xajax.$('submitButton').value="http://blogbeta.blueidea.com/wait...";
//修改id为submitButton的属性
xajax_processForm(xajax.getFormValues("signupForm"));
//这里xajax_ 后面跟要使用哪个函数这里是 processForm,后面跟的是signupForm这个表单各项的集合
return false;
}
</scrīpt>
</head>
<body><form id="signupForm" action="javascrīpt:void(null);" ōnSubmit="submitSignup();">
<div id="main">
<div id="m1">用 户 注 册</div>
<div id="formDiv">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td align="right"> </td>
<td> </td>
</tr>
<tr>
<td width="31%" align="right">用户名:</td>
<td width="69%"><input name="usr" type="text" id="usr" />
*</td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input name="pwd" type="password" id="pwd" />
*</td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input name="pwd2" type="password" id="pwd2" />
*</td>
</tr>
<tr>
<td align="right">Email:</td>
<td><input name="email" type="text" id="email" />
* 可用于找回密码</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="submitButton" value="提交" class="button" />
<input type="reset" name="Submit2" value="重置" class="button" /></td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
点提交后执行singup.php里的processForm函数
3、inc/singup.php
<?php
define ('XAJAX_DEFAULT_CHAR_ENCODING', 'gb2312' );
//注意这里,必须要设置gb2312,不然中文会乱码
require_once("xajax.inc.php");
require_once("function.php");
$xajax = new xajax();
$xajax->registerFunction("processForm");
//同reg.php文件
function processForm($aFormValues)
{
$objResponse = new xajaxResponse();
require_once("conn.php");
$usr=$aFormValues['usr'];
$email=$aFormValues['email'];
$pwd=$aFormValues['pwd'];
$pw=Md5($pwd);
$errmsg="";
//要过滤的非法字符
$ArrFiltrate=array("'",";","union");
foreach($aFormValues as $key=>$value){
if (FunStringExist($value,$ArrFiltrate)){
$objResponse->addAlert("输入的信息含有非法字符\"' ; union!\"");
$objResponse->addAssign("submitButton","value","继续");
$objResponse->addAssign("submitButton","disabled",false);
return $objResponse;
}
}
if (trim($usr) == "")
{
$errmsg.="请输入用户名!\n";
}
if (trim($pwd) == "")
{
$errmsg.="请输入密码!\n";
}
if ($pwd != $aFormValues['pwd2'])
{
$errmsg.="两次输入的密码不一致!\n";
}
if (!CheckEmailAddr($email))
{
$errmsg.="邮件地址不正确!\n";
}
$sql="select * from zl_usr where zl_usr='$usr'";
$result=mysql_query($sql,$db);
if($myrow=mysql_fetch_array($result)){
$errmsg.="用户名已经存在!\n";
}
if ($errmsg=="")
{
$sForm = "注册成功<br>用户名:".$usr."<br>email:".$email."";
$sql="insert into zl_usr(zl_usr,zl_pwd,email) values('$usr','$pw','$email')";
$result=mysql_query($sql,$db);
$objResponse->addAssign("formDiv","innerHTML",$sForm);
}
else
{
$objResponse->addAlert($errmsg);
//弹出错误信息
$objResponse->addAssign("submitButton","value","继续");
//修改submitButton的value为继续
$objResponse->addAssign("submitButton","disabled",false);
//修改submitButton这个按扭的属性
}
return $objResponse;
}
$xajax->processRequests();
?>
这个文件中对信息进行了有效性判断,包括:用户名是否已经注册过、信息中是否有非法字符、邮件地址是否正确、两次输入的密码是否一致,如果没有错误则输入到数据库中,并
$objResponse->addAssign("formDiv","innerHTML",$sForm);
在formDiv中重新插入代码,内容为$sForm
$sForm = "注册成功<br>用户名:".$usr."<br>email:".$email."";
如果有错误信息则
$objResponse->addAlert($errmsg);
//弹出错误信息
$objResponse->addAssign("submitButton","value","继续");
$objResponse->addAssign("submitButton","disabled",false);
//修改submitButton这个按扭的属性
3、login.php 登陆文件
<?php
require_once("inc/xajax.inc.php");
$xajax = new xajax("inc/login.php");
$xajax->registerFunction("processForm");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" href="http://blogbeta.blueidea.com/css/style.css" _fcksavedurl=""http://blogbeta.blueidea.com/css/style.css"" type="text/css">
<title>无标题文档</title>
<?php $xajax->printJavascrīpt('inc/'); ?>
<scrīpt type="text/javascrīpt">
function submitSignup()
{
xajax.$('submitButton').disabled=true;
xajax.$('submitButton').value="http://blogbeta.blueidea.com/wait...";
xajax_processForm(xajax.getFormValues("signupForm"));
return false;
}
</scrīpt>
</head>
<body><form id="signupForm" action="javascrīpt:void(null);" ōnSubmit="submitSignup();">
<div id="main">
<div id="m1">用 户 登 陆</div>
<div id="formDiv">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td align="right"> </td>
<td> </td>
</tr>
<tr>
<td width="31%" align="right">用户名:</td>
<td width="69%"><input name="usr" type="text" id="usr" />
*</td>
</tr>
<tr>
<td align="right">密码:</td>
<td><input name="pwd" type="password" id="pwd" />
*</td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="submitButton" value="提交" class="button" />
<input type="reset" name="Submit2" value="重置" class="button" /></td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
4、inc/login.php 登陆用的处理文件
<?php
define ('XAJAX_DEFAULT_CHAR_ENCODING', 'gb2312' );
require_once("xajax.inc.php");
require_once("function.php");
$xajax = new xajax();
$xajax->registerFunction("processForm");
function processForm($aFormValues)
{
$objResponse = new xajaxResponse();
require_once("conn.php");
$usr=$aFormValues['usr'];
$email=$aFormValues['email'];
$pwd=$aFormValues['pwd'];
$pw=Md5($pwd);
$errmsg="";
//要过滤的非法字符
$ArrFiltrate=array("'",";","union");
foreach($aFormValues as $key=>$value){
if (FunStringExist($value,$ArrFiltrate)){
$objResponse->addAlert("输入的信息含有非法字符\"' ; union!\"");
$objResponse->addAssign("submitButton","value","继续");
$objResponse->addAssign("submitButton","disabled",false);
return $objResponse;
}
}
if (trim($usr) == "")
{
$errmsg.="请输入用户名!\n";
}
if (trim($pwd) == "")
{
$errmsg.="请输入密码!\n";
}
$sql="select * from zl_usr where zl_usr='$usr' and zl_pwd='$pw'";
$result=mysql_query($sql,$db);
if(!$myrow=mysql_fetch_array($result)){
$errmsg.="用户名不存在,或密码错误!\n";
}
if ($errmsg=="")
{
$sForm = "登陆成功";
$objResponse->addAssign("formDiv","innerHTML",$sForm);
}
else
{
$objResponse->addAlert($errmsg);
$objResponse->addAssign("submitButton","value","继续");
$objResponse->addAssign("submitButton","disabled",false);
}
return $objResponse;
}
$xajax->processRequests();
?>
登陆于注册原理差不多,就不废话了:)
另外下面是两个用到的文件代码 conn.php function.php
conn.php
<?php
$database="zl";//MYSQL数据库名
$db = mysql_connect("127.0.0.1", "root","123456");//MYSQL数据库用户名和密码
mysql_select_db($database,$db);
?>
function.php
<?php
function CheckEmailAddr($C_mailaddr)
{
if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$",
$C_mailaddr))
{
return false;
}
return true;
}
//是否存在数组中的值
function FunStringExist($StrFiltrate,$ArrFiltrate){
foreach ($ArrFiltrate as $key=>$value){
if (eregi($value,$StrFiltrate)){
return true;
}
}
return false;
}
?>