既然选择了远方,便只顾风雨兼程~~~

smarty结合xajax检测用户名简单实例

上一篇 / 下一篇  2006-12-20 21:39:51

查看( 10255 ) / 评论( 30 )
在原创那边写了几个phpajax的应用例子,今天和新手谈谈smarty+xjax,希望对新手有帮助,xajax是用PHP写的ajax开发框架,可以生成JS代码,这样使用起ajax就比较简单了,今天结合模板引擎smarty,来实现一个检测用户名合法性的小程序,大家有兴趣的话还可以扩展这个程序到自己的应用中,嗯,这里写出核心代码,里面注释很详细,不过建议大家看之前还是看看这个http://blog.csdn.net/fhiesc/archive/2006/07/04/873441.aspx,相信你会很快明白xajax是什么东东,及如何使用,最后依然是效果图和源代码下载。好的,看代码吧:
[php]
<?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; //设置缓存方式
  /*****************************************************
  左右边界符,默认为{},但实际应用当中容易与Javascrīpt
  相冲突,所以建议设成<{}>或其它。
  *****************************************************/
  $smarty->left_delimiter = "<{";
  $smarty->right_delimiter = "}>";


  $xajax->registerFunction("checkusername");//注册checkusername函数
  $xajax->processRequests();//调用xajax用于接管请求
  
  $smarty->assign('xajax_javascrīpt', $xajax->getJavascrīpt('./xajax/'));//输出JS代码,注意('./xajax/')中参数为xajax.inc.php父目录,在同意目录下可不同填写,否则必须填写
  $smarty->assign('title','smarty结合xajax检测用户名简单实例');//替换模板内容
  $smarty->display('index.tpl');//显示模板内容
?>
[/php]
模板代码:
[php]
<html>
<head>
<title><{$title}></title>
<{$xajax_javascrīpt}><{*使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>
[/php]
是不是不错?喜欢就顶吧~~~~~~~
效果图:

[本帖最后由 leehui1983 于 2006-12-10 10:49 编辑]

1.jpg

2.jpg

3.jpg

smartyxajax.rar
(2006-12-09 15:53:14, Size: 145 kB, Downloads: 36)


TAG:

86800787的个人空间 86800787 发布于2006-12-09 16:05:58

QUOTE:

leehui1983 (辉老大)
偶像!!!
fengyun的个人空间 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
PHP流浪者(cator) cator 发布于2006-12-09 21:25:43

志凡的个人空间 dzjzmj 发布于2006-12-10 01:04:04
强烈支持
辉老大的地盘 leehui1983 发布于2006-12-10 11:52:17
多谢支持:lol
oujianhui的个人空间 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的个人空间 loveyou_85 发布于2006-12-18 17:13:00
学习了,3Q
oujianhui的个人空间 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 编辑 ]
2.gif

2.gif

1.gif

1.gif

3.gif

3.gif

4.gif

4.gif

5.gif

5.gif

我的PHP世界 115300111 发布于2006-12-19 16:13:19
这么厉害!!!
wlsuo发布于2007-04-02 14:50:15
受益匪浅
城市蓝色街灯 linuxroot 发布于2007-07-05 12:01:18
顶一个
qqsheji的个人空间 qqsheji 发布于2007-07-29 00:26:47
非常感谢您的 好东西
liduanshi发布于2007-08-29 15:44:48
很好~ 不错
还没顶过帖子,这个写的真是很好,言简意赅!不错!帮你顶!
我来说两句

(可选)

Open Toolbar