PHP&AJAX無刷新de註冊 好像不好用

./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 php
AJAX實現無刷新註冊
*@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 php
AJAX實現無刷新註冊
*@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 php
AJAX實現無刷新註冊
*@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 php
AJAX實現無刷新註冊
*@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();
        }
    }

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章