PHP+AJAX實現無重新整理註冊(帶使用者名稱實時檢測)程式碼
阿新 • • 發佈:2019-01-09
很多時候,我們在網上註冊個人資訊,在提交完頁面後,總得等待頁面重新整理來告訴我們註冊是否成功,遇到網路差的時候,如果註冊了一大串的東西,在經過漫長的等待頁面重新整理後,得到的確是“您的使用者名稱已被使用”或XXXXXXX不合法,我想大家的心情一定特別不爽,今天就介紹個AJAX實現頁面不重新整理註冊+實時檢測使用者資訊的簡單註冊程式,希望對大家有所幫助。好的,先看註冊介面程式碼:
如圖:
檔案包括:
- userreg.html ( 註冊頁面)
- ajaxreg .js(AJAX指令碼及實時驗證的JS指令碼)
- checkuserreg .php(連線資料庫並檢測使用者名稱是否已註冊的頁面)
- senduserinfo.php(資訊合法後我們提交註冊資訊實現無重新整理註冊的PHP程式碼)
程式碼如下:(只貼上關鍵部分以便理解)
userreg.html ( 註冊頁面)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文件</title> <script language="javascript" src="ajaxreg.js"></script> <script language="JavaScript" type="text/JavaScript"> function check(){ //使用者名稱為空的時候 if(document.reg.username.value==""){ document.getElementById('check').innerHTML=" <font color=red>使用者名稱不能為空!</font>"; document.reg.username.focus(); return false; } if(document.getElementById('check').innerHTML==" <font color=red>The number is registed</font>"){ //使用者名稱已被註冊的時候(<font color=red>The number is registed</font>是AJAX返回回來的) document.reg.username.focus(); return false; } if(document.reg.userpwd.value==""){ //密碼為空的時候 document.getElementById('pwd').innerHTML=" <font color=red>使用者密碼不能為空!</font>"; document.reg.userpwd.focus(); return false; } if(document.reg.userpwd.value.length<6){ //密碼長度錯誤的時候 document.getElementById('pwd').innerHTML=" <font color=red>密碼長度不能小於6位!</font>"; document.reg.userpwd.focus(); return false; } if (document.reg.reuserpwd.value==""){ //沒有再次輸入密碼的時候 document.getElementById('repwd').innerHTML=" <font color=red>請再輸入一次密碼!</font>"; document.reg.reuserpwd.focus(); return false; } if(document.reg.userpwd.value!=document.reg.reuserpwd.value){ //再次輸入密碼不正確的時候 document.getElementById('repwd').innerHTML=" <font color=red>兩次輸入的密碼不一致!</font>"; document.reg.reuserpwd.focus(); return false; } return true; //上面的驗證都通過則return true,頁面資訊將被POST到checkuserreg.php進行處理。 } </script> </head> <body> <form method="post" name="reg" id="reg" action="checkuserreg.php"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tr> <td class="td1">登陸帳號:</td> <td> <input name="username" type="text" id="username" size="20" maxlength="20" class="textipt"/> <span id="check" class="msg">4-16個字元,英文小寫、漢字、數字。</span></td> </tr> <tr> <td class="td1">登入密碼:</td> <td><input name="userpwd" type="password" id="userpwd" size="30" maxlength="30" class="textipt"/> <span class="msg" id="pwd">密碼字母有大小寫之分,英文、數字。</span></td> </tr> <tr> <td class="td1">確認密碼:</td> <td><input name="reuserpwd" type="password" id="reuserpwd" size="30" maxlength="30" class="textipt"/> <span class="msg" id="repwd">請再次輸入登入密碼。</span></td> </tr> <tr> <td colspan="2" align="left" valign="middle" height="50"> <input name="submit" type="image" src="img/reg_but1.jpg" /></td> </tr> </table> </form> </body> </html>
ajaxreg .js程式碼(AJAX指令碼及實時驗證的JS指令碼)
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>"; } }
checkuserreg .php程式碼:(連線資料庫並檢測使用者名稱是否已註冊的頁面)
<?php
header('Content-Type:text/html;charset=GB2312');//避免輸出亂碼
include('inc/config.inc.php');//包含資料庫基本配置資訊
include('inc/dbclass.php');//包含資料庫操作類
username=trim(_GET['username']);//獲取註冊名
//-----------------------------------------------------------------------------------
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>此會員名已被註冊,請更換會員名!</font>";
}
//會員名未註冊則顯示
//----------------------------------------------------------------------------
else{
echo" <font color=red>此會員名可以註冊!</font>";
}
if(action==reg){
addsql="insert into cr_userinfo
values(0,'username','userpwd','time',50,1,'userquestion','useranswer')";
db->query(addsql);
echo"<img src=images/pass.gif> <font color=red>恭喜您,註冊成功!請點選<a href=login.php>這裡</a>登陸!</font>";
}
db->close();//關閉資料庫連線
?>
senduserinfo.php(資訊合法後我們提交註冊資訊實現無重新整理註冊的PHP程式碼):
<?php
header('Content-Type:text/html;charset=GB2312');//避免輸出亂碼
include('inc/config.inc.php');//包含資料庫基本配置資訊
include('inc/dbclass.php');//包含資料庫操作類
username=trim(_GET['username']);//獲取註冊名
userpwd=md5(trim(_GET['userpwd']));//獲取註冊密碼
time=date("Y-m-d");
//-----------------------------------------------------------------------------------
db=new db;//從資料庫操作類生成例項
db->mysql(dbhost,dbuser,dbpassword,dbname);//呼叫連線引數函式
db->createcon();//呼叫建立連線函式
//-----------------------------------------------------------------------------------
//開始插入資料
//-----------------------------------------------------------------------------------
addsql="insert into cr_userinfo values(0,'username','userpwd','time',50,1,'userquestion','useranswer')";
db->query(addsql);
echo"<img src=images/pass.gif> <font color=red>恭喜您,註冊成功!請點選<a href=login.php>這裡</a>登入!</font>";
db->close();//關閉資料庫連線
?>
OK!!大功告成,來看看效果圖:
1.
2.
3.
4.
5.