既然选择了远方,便只顾风雨兼程~~~
smarty结合xajax检测用户名简单实例
查看( 10255 ) /
评论( 30 )
TAG:
-
86800787 发布于2006-12-09 16:05:58
-
QUOTE:
leehui1983 (辉老大)
偶像!!!
-
fengyun 发布于2006-12-09 16:15:48
-

-
leehui1983 发布于2006-12-09 16:16:09
-
QUOTE:
86800787
多谢夸奖,没那么夸张啦~~~~~
[ 本帖最后由 leehui1983 于 2006-12-9 16:19 编辑 ]
-
leehui1983 发布于2006-12-09 16:40:21
-
不要沉,我顶~~~
-
suturn-ly发布于2006-12-09 20:17:22
-
帮顶...正在吸收中......:lol
-
cator 发布于2006-12-09 21:25:43
-

-
dzjzmj 发布于2006-12-10 01:04:04
-
强烈支持
-
leehui1983 发布于2006-12-10 11:52:17
-
多谢支持:lol

-
oujianhui 发布于2006-12-18 01:11:52
-
so nice
-
leehui1983 发布于2006-12-18 11:59:22
-
:$
-
dzjzmj 发布于2006-12-18 12:54:05
-
再次支持
-
leehui1983 发布于2006-12-18 13:04:33
-
QUOTE:
原帖由 dzjzmj 于 2006-12-18 12:54 发表
:lol
再次支持
-
黄健 发布于2006-12-18 16:48:25
-
8错,学习了
-
loveyou_85 发布于2006-12-18 17:13:00
-
学习了,3Q
-
oujianhui 发布于2006-12-18 21:20:13
-
这个例子不错,我做了一遍.感觉很不错.谢谢了.
现在,我扩展了部分功能:
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)事件能够检查输入。
五、附图片
[ 本帖最后由 oujianhui 于 2006-12-18 21:35 编辑 ]
-
115300111 发布于2006-12-19 16:13:19
-
这么厉害!!!
-
wlsuo发布于2007-04-02 14:50:15
-
受益匪浅
-
linuxroot 发布于2007-07-05 12:01:18
-
顶一个
-
qqsheji 发布于2007-07-29 00:26:47
-
非常感谢您的 好东西
-
liduanshi发布于2007-08-29 15:44:48
-
很好~ 不错
还没顶过帖子,这个写的真是很好,言简意赅!不错!帮你顶!









