PHP+AJAX實(shí)現(xiàn)無刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
更新時(shí)間:2006年12月02日 00:00:00 作者:
很多時(shí)候,我們?cè)诰W(wǎng)上注冊(cè)個(gè)人信息,在提交完頁面后,總得等待頁面刷新來告訴我們注冊(cè)是否成功,遇到網(wǎng)絡(luò)差的時(shí)候,如果注冊(cè)了一大串的東西,在經(jīng)過漫長的等待頁面刷新后,得到的確是“您的用戶名已被使用”或XXXXXXX不合法,我想大家的心情一定特別不爽,今天就介紹個(gè)AJAX實(shí)現(xiàn)頁面不刷新注冊(cè)+實(shí)時(shí)檢測(cè)用戶信息的簡單注冊(cè)程序,希望對(duì)大家有所幫助。好的,先看注冊(cè)界面代碼:
<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>
<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" >
<font color="#FF6633">*</font></td>
<td align="left" bgcolor="#FFFFFF" id="check"> 4-16個(gè)字符,英文小寫、漢字、數(shù)字、最好不要全部是數(shù)字。</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" >
<font color="#FF6633">*</font> </td>
<td align="left" bgcolor="#FFFFFF" id="pwd"> 密碼字母有大小寫之分。6-16位(包括6、16),限用英文、數(shù)字。</td>
</tr>
<tr>
<td width="150" align="left" bgcolor="#FFFFFF"> · 重復(fù)密碼:</td>
<td width="310" align="center" bgcolor="#FFFFFF">
<input name="reuserpwd" type="password" class="inputtext" id="reuserpwd" >
<font color="#FF6633">*</font> </td>
<td align="left" bgcolor="#FFFFFF" id="repwd"> 請(qǐng)?jiān)俅屋斎氲卿浢艽a。</td>
</tr>
</table>
如圖:

紅色部分就是一會(huì)要調(diào)用的js函數(shù)了,當(dāng)我們選定一個(gè)文本框后就會(huì)開始調(diào)用,現(xiàn)在我們看上面那個(gè)頁面包含的ajaxreg.js文件的代碼,里面就是包含了ajax框架和一些判斷函數(shù)。
var http_request=false;
function send_request(url){//初始化,指定處理函數(shù),發(fā)送請(qǐng)求的函數(shù)
http_request=false;
//開始初始化XMLHttpRequest對(duì)象
if(window.XMLHttpRequest){//Mozilla瀏覽器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//設(shè)置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){//異常,創(chuàng)建對(duì)象實(shí)例失敗
window.alert("創(chuàng)建XMLHttp對(duì)象失?。?);
return false;
}
http_request.onreadystatechange=processrequest;
//確定發(fā)送請(qǐng)求方式,URL,及是否同步執(zhí)行下段代碼
http_request.open("GET",url,true);
http_request.send(null);
}
//處理返回信息的函數(shù)
function processrequest(){
if(http_request.readyState==4){//判斷對(duì)象狀態(tài)
if(http_request.status==200){//信息已成功返回,開始處理信息
document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//頁面不正常
alert("您所請(qǐng)求的頁面不正常!");
}
}
}
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="正在讀取數(shù)據(jù)...";
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>請(qǐng)?jiān)佥斎胍淮蚊艽a!</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>";
}
}
不難看出,數(shù)據(jù)是通過異步傳輸?shù)絚heckuserreg.php接著回送回信息顯示出來來達(dá)到實(shí)時(shí)檢測(cè)用戶名的目的,至于密碼,只作了長度的實(shí)時(shí)判斷,有興趣的朋友可以擴(kuò)充功能。來看下checkuserreg.php到底都做了什么:
<?php
header('Content-Type:text/html;charset=GB2312');//避免輸出亂碼
include('inc/config.inc.php');//包含數(shù)據(jù)庫基本配置信息
include('inc/dbclass.php');//包含數(shù)據(jù)庫操作類
$username=trim($_GET['username']);//獲取注冊(cè)名
//-----------------------------------------------------------------------------------
$db=new db;//從數(shù)據(jù)庫操作類生成實(shí)例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//調(diào)用連接參數(shù)函數(shù)
$db->createcon();//調(diào)用創(chuàng)建連接函數(shù)
//-----------------------------------------------------------------------------------
$querysql="select username from cr_userinfo where username='$username'";//查詢會(huì)員名
$result=$db->query($querysql);
$rows=$db->loop_query($result);
//若會(huì)員名已注冊(cè)
//-----------------------------------------------------------------------------------
if($rows){
echo" <font color=red>此會(huì)員名已被注冊(cè),請(qǐng)更換會(huì)員名!</font>";
}
//會(huì)員名未注冊(cè)則顯示
//-----------------------------------------------------------------------------------
else{
echo" <font color=red>此會(huì)員名可以注冊(cè)!</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>恭喜您,注冊(cè)成功!請(qǐng)點(diǎn)擊<a href=login.php>這里</a>登陸!</font>";
}
$db->close();//關(guān)閉數(shù)據(jù)庫連接
?>
注釋寫的還算詳細(xì),大家應(yīng)該都能看懂,再看信息合法后我們提交注冊(cè)信息實(shí)現(xiàn)無刷新注冊(cè)的PHP代碼,senduserinfo.php:
<?php
header('Content-Type:text/html;charset=GB2312');//避免輸出亂碼
include('inc/config.inc.php');//包含數(shù)據(jù)庫基本配置信息
include('inc/dbclass.php');//包含數(shù)據(jù)庫操作類
$username=trim($_GET['username']);//獲取注冊(cè)名
$userpwd=md5(trim($_GET['userpwd']));//獲取注冊(cè)密碼
$time=date("Y-m-d");
//-----------------------------------------------------------------------------------
$db=new db;//從數(shù)據(jù)庫操作類生成實(shí)例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//調(diào)用連接參數(shù)函數(shù)
$db->createcon();//調(diào)用創(chuàng)建連接函數(shù)
//-----------------------------------------------------------------------------------
//開始插入數(shù)據(jù)
//-----------------------------------------------------------------------------------
$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>恭喜您,注冊(cè)成功!請(qǐng)點(diǎn)擊<a href=login.php>這里</a>登錄!</font>";
$db->close();//關(guān)閉數(shù)據(jù)庫連接
?>
OK?。〈蠊Ω娉?,來看看效果圖:
1.

2.

3.

4.

5.

怎么樣?還不錯(cuò)吧,貼了這么多累死了,希望大家喜歡~~~~
<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>
<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" >
<font color="#FF6633">*</font></td>
<td align="left" bgcolor="#FFFFFF" id="check"> 4-16個(gè)字符,英文小寫、漢字、數(shù)字、最好不要全部是數(shù)字。</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" >
<font color="#FF6633">*</font> </td>
<td align="left" bgcolor="#FFFFFF" id="pwd"> 密碼字母有大小寫之分。6-16位(包括6、16),限用英文、數(shù)字。</td>
</tr>
<tr>
<td width="150" align="left" bgcolor="#FFFFFF"> · 重復(fù)密碼:</td>
<td width="310" align="center" bgcolor="#FFFFFF">
<input name="reuserpwd" type="password" class="inputtext" id="reuserpwd" >
<font color="#FF6633">*</font> </td>
<td align="left" bgcolor="#FFFFFF" id="repwd"> 請(qǐng)?jiān)俅屋斎氲卿浢艽a。</td>
</tr>
</table>
如圖:
紅色部分就是一會(huì)要調(diào)用的js函數(shù)了,當(dāng)我們選定一個(gè)文本框后就會(huì)開始調(diào)用,現(xiàn)在我們看上面那個(gè)頁面包含的ajaxreg.js文件的代碼,里面就是包含了ajax框架和一些判斷函數(shù)。
var http_request=false;
function send_request(url){//初始化,指定處理函數(shù),發(fā)送請(qǐng)求的函數(shù)
http_request=false;
//開始初始化XMLHttpRequest對(duì)象
if(window.XMLHttpRequest){//Mozilla瀏覽器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//設(shè)置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){//異常,創(chuàng)建對(duì)象實(shí)例失敗
window.alert("創(chuàng)建XMLHttp對(duì)象失?。?);
return false;
}
http_request.onreadystatechange=processrequest;
//確定發(fā)送請(qǐng)求方式,URL,及是否同步執(zhí)行下段代碼
http_request.open("GET",url,true);
http_request.send(null);
}
//處理返回信息的函數(shù)
function processrequest(){
if(http_request.readyState==4){//判斷對(duì)象狀態(tài)
if(http_request.status==200){//信息已成功返回,開始處理信息
document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//頁面不正常
alert("您所請(qǐng)求的頁面不正常!");
}
}
}
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="正在讀取數(shù)據(jù)...";
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>請(qǐng)?jiān)佥斎胍淮蚊艽a!</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>";
}
}
不難看出,數(shù)據(jù)是通過異步傳輸?shù)絚heckuserreg.php接著回送回信息顯示出來來達(dá)到實(shí)時(shí)檢測(cè)用戶名的目的,至于密碼,只作了長度的實(shí)時(shí)判斷,有興趣的朋友可以擴(kuò)充功能。來看下checkuserreg.php到底都做了什么:
<?php
header('Content-Type:text/html;charset=GB2312');//避免輸出亂碼
include('inc/config.inc.php');//包含數(shù)據(jù)庫基本配置信息
include('inc/dbclass.php');//包含數(shù)據(jù)庫操作類
$username=trim($_GET['username']);//獲取注冊(cè)名
//-----------------------------------------------------------------------------------
$db=new db;//從數(shù)據(jù)庫操作類生成實(shí)例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//調(diào)用連接參數(shù)函數(shù)
$db->createcon();//調(diào)用創(chuàng)建連接函數(shù)
//-----------------------------------------------------------------------------------
$querysql="select username from cr_userinfo where username='$username'";//查詢會(huì)員名
$result=$db->query($querysql);
$rows=$db->loop_query($result);
//若會(huì)員名已注冊(cè)
//-----------------------------------------------------------------------------------
if($rows){
echo" <font color=red>此會(huì)員名已被注冊(cè),請(qǐng)更換會(huì)員名!</font>";
}
//會(huì)員名未注冊(cè)則顯示
//-----------------------------------------------------------------------------------
else{
echo" <font color=red>此會(huì)員名可以注冊(cè)!</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>恭喜您,注冊(cè)成功!請(qǐng)點(diǎn)擊<a href=login.php>這里</a>登陸!</font>";
}
$db->close();//關(guān)閉數(shù)據(jù)庫連接
?>
注釋寫的還算詳細(xì),大家應(yīng)該都能看懂,再看信息合法后我們提交注冊(cè)信息實(shí)現(xiàn)無刷新注冊(cè)的PHP代碼,senduserinfo.php:
<?php
header('Content-Type:text/html;charset=GB2312');//避免輸出亂碼
include('inc/config.inc.php');//包含數(shù)據(jù)庫基本配置信息
include('inc/dbclass.php');//包含數(shù)據(jù)庫操作類
$username=trim($_GET['username']);//獲取注冊(cè)名
$userpwd=md5(trim($_GET['userpwd']));//獲取注冊(cè)密碼
$time=date("Y-m-d");
//-----------------------------------------------------------------------------------
$db=new db;//從數(shù)據(jù)庫操作類生成實(shí)例
$db->mysql($dbhost,$dbuser,$dbpassword,$dbname);//調(diào)用連接參數(shù)函數(shù)
$db->createcon();//調(diào)用創(chuàng)建連接函數(shù)
//-----------------------------------------------------------------------------------
//開始插入數(shù)據(jù)
//-----------------------------------------------------------------------------------
$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>恭喜您,注冊(cè)成功!請(qǐng)點(diǎn)擊<a href=login.php>這里</a>登錄!</font>";
$db->close();//關(guān)閉數(shù)據(jù)庫連接
?>
OK?。〈蠊Ω娉?,來看看效果圖:
1.
2.
3.
4.
5.
怎么樣?還不錯(cuò)吧,貼了這么多累死了,希望大家喜歡~~~~
您可能感興趣的文章:
- 利用Ajax檢測(cè)用戶名是否被占用的完整實(shí)例
- JQuery Ajax如何實(shí)現(xiàn)注冊(cè)檢測(cè)用戶名
- PHP+Ajax實(shí)現(xiàn)的檢測(cè)用戶名功能簡單示例
- AJAX應(yīng)用實(shí)例之檢測(cè)用戶名是否唯一(實(shí)例代碼)
- AJAX實(shí)現(xiàn)無刷新檢測(cè)用戶名功能
- jQuery+Ajax實(shí)現(xiàn)用戶名重名實(shí)時(shí)檢測(cè)
- 使用Ajax實(shí)時(shí)檢測(cè)"用戶名、郵箱等"是否已經(jīng)存在
- PHP+Ajax檢測(cè)用戶名或郵件注冊(cè)時(shí)是否已經(jīng)存在實(shí)例教程
- Asp.net下利用Jquery Ajax實(shí)現(xiàn)用戶注冊(cè)檢測(cè)(驗(yàn)證用戶名是否存)
- jquery ajax 檢測(cè)用戶注冊(cè)時(shí)用戶名是否存在
- PHP+AJAX實(shí)現(xiàn)無刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
- ajax 檢測(cè)用戶名是否被占用
- AJAX檢測(cè)用戶名是否存在的方法
相關(guān)文章
php中使用addslashes函數(shù)報(bào)錯(cuò)問題的解決方法
php中使用addslashes函數(shù)報(bào)錯(cuò)問題的解決方法,需要的朋友可以參考下2013-02-02Swoole 1.10.0新版本發(fā)布,增加了多項(xiàng)新特性
最近Swoole 1.10.0版本發(fā)布了,增加多項(xiàng)新特性,所以下面這篇文章主要給大家介紹了關(guān)于Swoole 1.10.0版本中新特性的相關(guān)資料,分享出來供大家參考學(xué)習(xí),需要的朋友可以參考下2018-01-01PHP實(shí)現(xiàn)JS中escape與unescape的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)JS中escape與unescape的方法,通過json_encode和json_decode方法實(shí)現(xiàn)JS中escape與unescape函數(shù)的功能,需要的朋友可以參考下2016-07-07攻克CakePHP(PHP中的Ruby On Rails框架)圖文介紹
CakePHP框架實(shí)例介紹分析。圖文并茂2008-10-10php使用explode()函數(shù)將字符串拆分成數(shù)組的方法
這篇文章主要介紹了php使用explode()函數(shù)將字符串拆分成數(shù)組的方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02PHP設(shè)計(jì)模式之組合模式定義與應(yīng)用示例
這篇文章主要介紹了PHP設(shè)計(jì)模式之組合模式定義與應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了PHP組合模式基本原理、定義與使用方法,需要的朋友可以參考下2020-02-02