很多時候,我們在網上註冊個人資訊,在提交完頁面後,總得等待頁面重新整理來告訴我們註冊是否成功,遇到網路差的時候,如果註冊了一大串的東西,在經過漫長的等待頁面重新整理後,得到的確是“您的使用者名稱已被使用”或XXXXXXX不合法,我想大家的心情一定特別不爽,今天就介紹個AJAX實現頁面不重新整理註冊+即時檢測使用者資訊的簡單註冊程式,希望對大家有所協助。好的,先看註冊介面代碼:
| · 使用者名稱稱: |
* |
4-16個字元,英文小寫、漢字、數字、最好不要全部是數字。 |
| · 使用者密碼: |
* |
密碼字母有大小寫之分。6-16位(包括6、16),限用英文、數字。 |
| · 重複密碼: |
* |
請再次輸入登入密碼。 |
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4108996');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4108996" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
紅色部分就是一會要調用的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=" 使用者名稱不可為空!";
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=" 使用者密碼不可為空!";
f.userpwd.focus();
return false;
}
else if(f.userpwd.value.length<6){
document.getElementById(obj).innerHTML=" 密碼長度不能小於6位!";
f.userpwd.focus();
return false;
}
else{
document.getElementById(obj).innerHTML=" 密碼符合要求!";
}
}
function pwdrecheck(obj){
var f=document.reg;
var repwd=f.reuserpwd.value;
if (repwd==""){
document.getElementById(obj).innerHTML=" 請再輸入一次密碼!";
f.reuserpwd.focus();
return false;
}
else if(f.userpwd.value!=f.reuserpwd.value){
document.getElementById(obj).innerHTML=" 兩次輸入的密碼不一致!";
f.reuserpwd.focus();
return false;
}
else{
document.getElementById(obj).innerHTML=" 密碼輸入正確!";
}
}
不難看出,資料是通過非同步傳輸到checkuserreg.php接著回送回資訊顯示出來來達到即時檢測使用者名稱的目的,至於密碼,只作了長度的即時判斷,有興趣的朋友可以擴充功能。來看下checkuserreg.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" 此會員名已被註冊,請更換會員名!";
}
//會員名未註冊則顯示
//-----------------------------------------------------------------------------------
else{
echo" 此會員名可以註冊!";
}
if($action==reg){
$addsql="insert into cr_userinfo
values(0,'$username','$userpwd','$time',50,1,'$userquestion','$useranswer')";
$db->query($addsql);
echo" 恭喜您,註冊成功!請點擊這裡登陸!";
}
$db->close();//關閉資料庫連接
?>
注釋寫的還算詳細,大家應該都能看懂,再看資訊合法後我們提交註冊資訊實現無重新整理註冊的PHP代碼,senduserinfo.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" 恭喜您,註冊成功!請點擊這裡登入!";
$db->close();//關閉資料庫連接
?>
OK!!大功告成,來看看:
1.
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109258');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109258" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
2.
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109286');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109286" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
3.
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109299');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109299" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
4.
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109333');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109333" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
5.
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109336');}" alt="" src="http://leehui1983.bokee.com/photo/view.fcgi?mode=3&id=4109336" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" border=0>
怎麼樣?還不錯吧,貼了這麼多累死了,希望大家喜歡~~~~
http://www.bkjia.com/PHPjc/317173.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/317173.htmlTechArticle很多時候,我們在網上註冊個人資訊,在提交完頁面後,總得等待頁面重新整理來告訴我們註冊是否成功,遇到網路差的時候,如果註冊了一大...