在原创那边写了几个php+ajax的应用例子,今天和新手谈谈smarty+xjax,希望对新手有帮助,xajax是用PHP写的ajax开发框架,可以生成JS代码,这样使用起ajax就比较简单了,今天结合模板引擎smarty,来实现一个检测用户名合法性的小程序,大家有兴趣的话还可以扩展这个程序到自己的应用中,嗯,这里写出核心代码,里面注释很详细,不过建议大家看之前还是看看这个http://blog.csdn.net/fhiesc/archive/2006/07/04/873441.aspx,相信你会很快明白xajax是什么东东,及如何使用,最后依然是效果图和源代码下载。好的,看代码吧:
<?php
/*****************************************
Title :smarty结合xajax检测用户名简单实例
Author:leehui1983(辉老大)
Finish Date :2006-12-09
*****************************************/
//为避免中文乱码,需要在 xajax.inc.php 需要改一下默认的encoding:define ('XAJAX_DEFAULT_CHAR_ENCODING', 'gbk' )UTF8编码格式文件不需要更改
require_once('./libs/Smarty.class.php');//包含smarty类库
require('./xajax/xajax.inc.php');//包含xajax类库
function checkusername($textvalue){//编写需要的PHP函数
$checkresult=($textvalue=='test' ? '<font color=red>该用户名已经注册</font>' :'<font color=red>可以注册</font>');
$objresponse=new xajaxResponse();//实例化xajaxresponse对象
$objresponse->addassign("result","innerHTML",$checkresult);//指定ID为result的元素中添加内容$checkresult
return $objresponse;//返回结果文本
}
$xajax=new xajax();//实例化xajax对象
$smarty=new Smarty();//实例化smarty对象
$smarty->template_dir = "./templates";//设置模板目录
$smarty->compile_dir = "./templates_c"; //设置编译目录
$smarty->caching = false; //设置缓存方式
/*****************************************************
左右边界符,默认为{},但实际应用当中容易与JavaScript
相冲突,所以建议设成<{}>或其它。
*****************************************************/
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
$xajax->registerFunction("checkusername");//注册checkusername函数
$xajax->processRequests();//调用xajax用于接管请求
$smarty->assign('xajax_javascript', $xajax->getJavascript('./xajax/'));//输出JS代码,注意('./xajax/')中参数为xajax.inc.php父目录,在同意目录下可不同填写,否则必须填写
$smarty->assign('title','smarty结合xajax检测用户名简单实例');//替换模板内容
$smarty->display('index.tpl');//显示模板内容
?>
模板代码:
<html>
<head>
<title><{$title}></title>
<{$xajax_javascript}><{*使smarty支持xajax*}>
</head>
<body >
<form name="check" >
请输入用户名:
<input type="text" name="username" />
<input type="button" name="button" value="检查用户名" />
<div id="result"></div>
</form>
</body>
</html>
////////////////////////////////////////////////////
下面是另一网友的实例化:
扩展了部分功能:
1.检测数据库的用户名.
2.当鼠标离开用户名输入框,并点击用户名输入框外或点击下一下输入框时,自动检测用户名是否已经注册,而不用自己去点击"检测用户名".
我的运行环境:apache2.0+php5.1.16+MySQL5.0.17+samrty+xajax
一、文件目录(见下面图片)
二、建立数据库
CREATE TABLE `checkusername` (
`id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` CHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
三、文件代码
1、index.php
<?php
/****************************************************************
* File name : index.php
* Title : use smarty and xajax
* author : *
* Created on 2006-12-18
***************************************************************/
error_reporting(E_ALL); //show mysql error,when file finished,you can change it
define('ROOT_PATH' , ''); //define root file
define('XAJAX_DEFAULT_CHAR_ENCODING','utf-8'); //set the character of xajax
header('Content-Type: text/html;charset = utf-8'); //set the character
require_once(ROOT_PATH . 'includes/config.inc.php'); //define database
require_once(ROOT_PATH . 'includes/function.inc.php'); //some functions
//use xajax
require_once('xajax/xajax.inc.php'); //include xajax Class
//function check username
function checkusername($testvalue)
{
$sql = "SELECT id FROM `checkusername` WHERE `username`='{$testvalue}'";
$query_id = mysql_query($sql);
if(!mysql_errno())
{
$rows = mysql_num_rows($query_id);
$checkresult = ($rows ? '<font color=red>该用户已经注册</font>' : '<font color=red>可以注册</font>');
$objreponse = new xajaxresponse();
$objreponse->addAssign('result','innerHTML',$checkresult); //return result
return $objreponse;
}
else
{
print_mysql_error(); //this function in config.inc.php
}
}
mysql_connect(DB_HOST , DB_USER , DB_PASSWORD) or die('连接' . DB_HOST . '失败');
mysql_select_db(DB_NAME) or die('连接' . DB_NAME . '失败');
//set xajax
$xajax = new xajax();
$xajax->registerFunction('checkusername');
$xajax->processRequests();
//close database
mysql_close();
//set smarty
$template_name = 'first'; //the templates address
require_once(ROOT_PATH . 'includes/smarty/Smarty.class.php');
$smarty = new smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->caching = false;
$smarty->template_dir = "template/{$template_name}";
$smarty->compile_dir = 'template_c';
$smarty->left_delimiter = '[##'; //smarty's left delimiter,you can also use '<{'
$smarty->right_delimiter = '##]'; //smarty's right delimiter
//assign smarty
$smarty->assign('xajax_javaScript',$xajax->getJavascript('xajax/'));
$smarty->assign('title','smarty and xajax check username');
//print smarty
$smarty->display('index.tpl');
2、config.inc.php
<?php
/****************************************************************
* File name : config.inc.php
* Title : set database define
* author :
* Created on 2006-12-18
***************************************************************/
define('DB_HOST' , 'localhost'); //the server
define('DB_USER' , 'root'); //the username
define('DB_PASSWORD' , ''); //the password
define('DB_NAME' , 'checkusername'); //the database
define('IN' , true); //the check right
3、function.inc.php
<?php
/****************************************************************
* File name : function.inc.php
* Title : functions
* author :
* Created on 2006-12-18
***************************************************************/
//print mysql_error
function print_mysql_error()
{
echo 'mysql errno: ' . mysql_errno() . '<br />'; //error number
echo mysql_error(); //error content
mysql_close(); //close server
die(); //exit
}
4、index.tpl
<html>
<head>
<title>[##$title##]</title>
<meta http-equiv="Content-Type" c>
[##$xajax_javaScript##]
</head>
<body >
<form name="check">
用户名:<input name="username" >
<div id="result"></div>
</form>
</body>
</html>
四、对文件index.tpl的说明
index.tpl,并没有按纽“检测用户名”。用了onBlur事件处理
onBlur事件:当用户离开输入框时,一个模糊(blur)事件能够检查输入。
<?php
/*****************************************
Title :smarty结合xajax检测用户名简单实例
Author:leehui1983(辉老大)
Finish Date :2006-12-09
*****************************************/
//为避免中文乱码,需要在 xajax.inc.php 需要改一下默认的encoding:define ('XAJAX_DEFAULT_CHAR_ENCODING', 'gbk' )UTF8编码格式文件不需要更改
require_once('./libs/Smarty.class.php');//包含smarty类库
require('./xajax/xajax.inc.php');//包含xajax类库
function checkusername($textvalue){//编写需要的PHP函数
$checkresult=($textvalue=='test' ? '<font color=red>该用户名已经注册</font>' :'<font color=red>可以注册</font>');
$objresponse=new xajaxResponse();//实例化xajaxresponse对象
$objresponse->addassign("result","innerHTML",$checkresult);//指定ID为result的元素中添加内容$checkresult
return $objresponse;//返回结果文本
}
$xajax=new xajax();//实例化xajax对象
$smarty=new Smarty();//实例化smarty对象
$smarty->template_dir = "./templates";//设置模板目录
$smarty->compile_dir = "./templates_c"; //设置编译目录
$smarty->caching = false; //设置缓存方式
/*****************************************************
左右边界符,默认为{},但实际应用当中容易与JavaScript
相冲突,所以建议设成<{}>或其它。
*****************************************************/
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
$xajax->registerFunction("checkusername");//注册checkusername函数
$xajax->processRequests();//调用xajax用于接管请求
$smarty->assign('xajax_javascript', $xajax->getJavascript('./xajax/'));//输出JS代码,注意('./xajax/')中参数为xajax.inc.php父目录,在同意目录下可不同填写,否则必须填写
$smarty->assign('title','smarty结合xajax检测用户名简单实例');//替换模板内容
$smarty->display('index.tpl');//显示模板内容
?>
模板代码:
<html>
<head>
<title><{$title}></title>
<{$xajax_javascript}><{*使smarty支持xajax*}>
</head>
<body >
<form name="check" >
请输入用户名:
<input type="text" name="username" />
<input type="button" name="button" value="检查用户名" />
<div id="result"></div>
</form>
</body>
</html>
////////////////////////////////////////////////////
下面是另一网友的实例化:
扩展了部分功能:
1.检测数据库的用户名.
2.当鼠标离开用户名输入框,并点击用户名输入框外或点击下一下输入框时,自动检测用户名是否已经注册,而不用自己去点击"检测用户名".
我的运行环境:apache2.0+php5.1.16+MySQL5.0.17+samrty+xajax
一、文件目录(见下面图片)
二、建立数据库
CREATE TABLE `checkusername` (
`id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` CHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
三、文件代码
1、index.php
<?php
/****************************************************************
* File name : index.php
* Title : use smarty and xajax
* author : *
* Created on 2006-12-18
***************************************************************/
error_reporting(E_ALL); //show mysql error,when file finished,you can change it
define('ROOT_PATH' , ''); //define root file
define('XAJAX_DEFAULT_CHAR_ENCODING','utf-8'); //set the character of xajax
header('Content-Type: text/html;charset = utf-8'); //set the character
require_once(ROOT_PATH . 'includes/config.inc.php'); //define database
require_once(ROOT_PATH . 'includes/function.inc.php'); //some functions
//use xajax
require_once('xajax/xajax.inc.php'); //include xajax Class
//function check username
function checkusername($testvalue)
{
$sql = "SELECT id FROM `checkusername` WHERE `username`='{$testvalue}'";
$query_id = mysql_query($sql);
if(!mysql_errno())
{
$rows = mysql_num_rows($query_id);
$checkresult = ($rows ? '<font color=red>该用户已经注册</font>' : '<font color=red>可以注册</font>');
$objreponse = new xajaxresponse();
$objreponse->addAssign('result','innerHTML',$checkresult); //return result
return $objreponse;
}
else
{
print_mysql_error(); //this function in config.inc.php
}
}
mysql_connect(DB_HOST , DB_USER , DB_PASSWORD) or die('连接' . DB_HOST . '失败');
mysql_select_db(DB_NAME) or die('连接' . DB_NAME . '失败');
//set xajax
$xajax = new xajax();
$xajax->registerFunction('checkusername');
$xajax->processRequests();
//close database
mysql_close();
//set smarty
$template_name = 'first'; //the templates address
require_once(ROOT_PATH . 'includes/smarty/Smarty.class.php');
$smarty = new smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->caching = false;
$smarty->template_dir = "template/{$template_name}";
$smarty->compile_dir = 'template_c';
$smarty->left_delimiter = '[##'; //smarty's left delimiter,you can also use '<{'
$smarty->right_delimiter = '##]'; //smarty's right delimiter
//assign smarty
$smarty->assign('xajax_javaScript',$xajax->getJavascript('xajax/'));
$smarty->assign('title','smarty and xajax check username');
//print smarty
$smarty->display('index.tpl');
2、config.inc.php
<?php
/****************************************************************
* File name : config.inc.php
* Title : set database define
* author :
* Created on 2006-12-18
***************************************************************/
define('DB_HOST' , 'localhost'); //the server
define('DB_USER' , 'root'); //the username
define('DB_PASSWORD' , ''); //the password
define('DB_NAME' , 'checkusername'); //the database
define('IN' , true); //the check right
3、function.inc.php
<?php
/****************************************************************
* File name : function.inc.php
* Title : functions
* author :
* Created on 2006-12-18
***************************************************************/
//print mysql_error
function print_mysql_error()
{
echo 'mysql errno: ' . mysql_errno() . '<br />'; //error number
echo mysql_error(); //error content
mysql_close(); //close server
die(); //exit
}
4、index.tpl
<html>
<head>
<title>[##$title##]</title>
<meta http-equiv="Content-Type" c>
[##$xajax_javaScript##]
</head>
<body >
<form name="check">
用户名:<input name="username" >
<div id="result"></div>
</form>
</body>
</html>
四、对文件index.tpl的说明
index.tpl,并没有按纽“检测用户名”。用了onBlur事件处理
onBlur事件:当用户离开输入框时,一个模糊(blur)事件能够检查输入。
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/2083/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2009-10-19 00:35
评论列表