现在的位置: 首页 > 综合 > 正文

PHP&AJAX无刷新de注册

2013年12月04日 ⁄ 综合 ⁄ 共 7554字 ⁄ 字号 评论关闭
./checkuserreg.php
./index.html
./inc/ajaxreg.js
./inc/config.inc.php
./inc/dbclass.php
######################################

*@Purpose phpAJAX实现无刷新注册
*@Author     Jackey Shi
*@Date        2007-04-18
*@FileName index.html

#######################################

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<scrīpt language="javascrīpt" src="./inc/ajaxreg.js"></scrīpt>
<table width="831" border="0" align="center" cellpadding="0" cellspacing="0">
<tr style="display:none">
    <td height="35" align="center" id="result"> </td>
</tr>
</table>
<form method="post" name="reg" id="reg" action="checkuserreg.php">
<table width="100%" height="256" border="0" align="center" cellpadding="1" cellspacing="1">
      <tr>
        <td width="150" align="left" bgcolor="#FFFFFF"> ·
用户名称:          </td>
        <td width="310" align="center" bgcolor="#FFFFFF">
          <input name="username" type="text" class="inputtext" id="username" ōnChange="usercheck('check')" ōnBlur="usercheck('check')">
        <font color="#FF6633">*</font></td>
        <td align="left" bgcolor="#FFFFFF" id="check"> 4-16
个字符,英文小写、汉字、数字、最好不要全部是数字。</td>
      </tr>
      <tr>
        <td width="150" align="left" bgcolor="#FFFFFF">   ·
用户密码:</td>
        <td width="310" align="center" bgcolor="#FFFFFF">
          <input name="userpwd" type="password" class="inputtext" id="userpwd" ōnChange="pwdcheck('pwd')" ōnBlur="pwdcheck('pwd')">
          <font color="#FF6633">*</font>        </td>
        <td align="left" bgcolor="#FFFFFF" id="pwd">
密码字母有大小写之分。6-16位(包括616),限用英文、数字。</td>
      </tr>
          <tr>
        <td width="150" align="left" bgcolor="#FFFFFF"> ·
重复密码:</td>
        <td width="310" align="center" bgcolor="#FFFFFF">
          <input name="reuserpwd" type="password" class="inputtext" id="reuserpwd" ōnChange="pwdrecheck('repwd')" ōnBlur="pwdrecheck('repwd')">
           <font color="#FF6633">*</font>        </td>
        <td align="left" bgcolor="#FFFFFF" id="repwd">
请再次输入登录密码。</td>
          </tr>
    </table>
    <input type="hidden" name="action" value="reg">
    <input type="submit" name="submit">
    </form>

#######################################

*@Purpose phpAJAX实现无刷新注册
*@Author     Jackey Shi
*@Date        2007-04-18
*@FileName ajaxreg.js
/*
当我们选定一个文本框后就会开始调用,现在我们看上面那个页面
包含的ajaxreg.js文件的代码,里面就是包含了ajax框架和一些判断函数。*/

#######################################
var http_request=false;
function send_request(url){//
初始化,指定处理函数,发送请求的函数
    http_request=false;
        //
开始初始化XMLHttpRequest对象
        if(window.XMLHttpRequest){//Mozilla
浏览器
         http_request=new XMLHttpRequest();
         if(http_request.overrideMimeType){//
设置MIME类别
           http_request.overrideMimeType("text/xml");
         }
        }
        else if(window.ActiveXObject){//IE
浏览器
         try{
          http_request=new ActiveXObject("Msxml2.XMLHttp");
         }catch(e){
          try{
          http_request=new ActiveXobject("Microsoft.XMLHttp");
          }catch(e){}
         }
    }
        if(!http_request){//
异常,创建对象实例失败
         window.alert("
创建XMLHttp对象失败!");
         return false;
        }
        http_request.onreadystatechange=processrequest;
        //
确定发送请求方式,URL,及是否同步执行下段代码
    http_request.open("GET",url,true);
        http_request.send(null);
}
//
处理返回信息的函数
function processrequest(){
   if(http_request.readyState==4){//
判断对象状态
     if(http_request.status==200){//
信息已成功返回,开始处理信息
          document.getElementById(reobj).innerHTML=http_request.responseText;
         }
         else{//
页面不正常
          alert("
您所请求的页面不正常!");
         }
   }
}
function usercheck(obj){
   var f=document.reg;
   var username=f.username.value;
   if(username==""){
   document.getElementById(obj).innerHTML=" <font color=red>
用户名不能为空!</font>";
        f.username.focus();
        return false;
   }
   else{
   document.getElementById(obj).innerHTML="
正在读取数据...";
   send_request('checkuserreg.php?username='+username);
   reobj=obj;
   }
}
function pwdcheck(obj){
    var f=document.reg;
        var pwd=f.userpwd.value;
        if(pwd==""){
          document.getElementById(obj).innerHTML=" <font color=red>
用户密码不能为空!</font>";
          //f.userpwd.focus();
          return false;
        }
        else if(f.userpwd.value.length<6){
          document.getElementById(obj).innerHTML=" <font color=red>
密码长度不能小于6位!</font>";
          //f.userpwd.focus();
          return false;
        }
        else{
          document.getElementById(obj).innerHTML=" <font color=red>
密码符合要求!</font>";
        }
}
function pwdrecheck(obj){
    var f=document.reg;
        var repwd=f.reuserpwd.value;
        if (repwd==""){
          document.getElementById(obj).innerHTML=" <font color=red>
请再输入一次密码!</font>";
          //f.reuserpwd.focus();
          return false;
        }
        else if(f.userpwd.value!=f.reuserpwd.value){
          document.getElementById(obj).innerHTML=" <font color=red>
两次输入的密码不一致!</font>";
         // f.reuserpwd.focus();
          return false;
        }
        else{
          document.getElementById(obj).innerHTML=" <font color=red>
密码输入正确!</font>";
        }
}

#######################################

*@Purpose phpAJAX实现无刷新注册
*@Author     Jackey Shi
*@Date        2007-04-18
*@FileName checkuserreg.php

//*
不难看出,数据是通过异步传输到checkuserreg.php接着回送回信息显示出来来达到实时检
测用户名的目的*/

#######################################
<?php
     header("Content-type: text/html;charset=utf-8");
   //header('Content-Type:text/html;charset=GB2312');//
避免输出乱码
   include('inc/config.inc.php');//
包含数据库基本配置信息
   include('inc/dbclass.php');//
包含数据库操作类
   $username=trim($_GET['username']);//
获取注册名
   //echo "<pre>";print_r($_POST);echo "</pre>";
   //-----------------------------------------------------------------------------------
   $db=new db;//
从数据库操作类生成实例
   #$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//
调用连接参数函数
   #$db->createcon();//
调用创建连接函数
   //-----------------------------------------------------------------------------------
   $querysql="select username from cr_userinfo where username='$username'";//
查询会员名
   $result=$db->query($querysql);
   $rows=$db->loop_query($result);
   //
若会员名已注册
   //-----------------------------------------------------------------------------------
   if($rows){
           echo" <font color=red>The number is registed</font>";
   }
   //
会员名未注册则显示
   //-----------------------------------------------------------------------------------
   else{
           echo" <font color=red>The number is
可以注册</font>";
   }
   if($_POST['action']==reg){
   $addsql="insert into cr_userinfo
           values('$_POST[username]','$_POST[userpwd]')";

   $db->query($addsql);
   echo"< img src=images/pass.gif> <font color=red>congratulations please link< a href=login.php>
这里</a>登陆!</font>";
   }
   //$db->close();//
关闭数据库连接
?>
#######################################

*@Purpose phpAJAX实现无刷新注册
*@Author     Jackey Shi
*@Date        2007-04-18
*@FileName config.inc.php

#######################################
<?php

/*************************
  
   
页面:config.inc.php
   
作者:Jackey Shi
   
功能:数据库参数变量设定
    $dbhost:
主机名
    $dbuser:
连接帐户
    $dbpassword:
连接密码
    $dbname:
数据库名

*************************/
    $dbhost      = "localhost";
    $dbuser      = "root";
    $dbpassword = "chinafu";
    $dbname      = "testdb";

?>
#######################################

*@Purpose phpAJAX实现无刷新注册
*@Author     Jackey Shi
*@Date        2007-04-18
*@FileName dbclass.php

#######################################
<?php

/**************************
   
  
页面:dbclass.php
  
作者:Jackey Shi
  
功能:定义数据库操作类

**************************/
class db{

       //创建构造函数,作用:数据库连接并选择相应数据库
       
public function __construct(){
            require(
'config.inc.php');
           
mysql_connect($dbhost,$dbuser,$dbpassword) or die("error!");
           
mysql_query("SET NAMES 'GBK'");
           
mysql_select_db($dbname);
        }

       //执行sql语句函数
       
public function query($sql){
            return
mysql_query($sql);
        }

       //取得结果集数组函数
       
public function loop_query($result){
            return
mysql_fetch_array($result);
        }

       //创建析构函数,作用:关闭数据库连接
       
public function __destruct(){
            return
mysql_close();
        }
    }

?>




 

抱歉!评论已关闭.