AJAX實(shí)現(xiàn)無刷新檢測(cè)用戶名功能
先來看看原理圖

register.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>ajax無刷新檢測(cè)</title>
<style type="text/css">
body{margin:0;padding:0;}.content{width:800px;margin:0 auto;}ul,li{list-style: none;margin:0;padding:0;}
tr{width:200px;}td{width:80px;padding:5px 0;}td input,textarea{border: 1px solid #79ABFE;}
</style>
</head>
<body>
<div class="content">
<script>
myXmlHttpRequest.ContentType=("text/xml;charset=UTF-8");
//創(chuàng)建ajax引擎(1號(hào)線)
function getXmlHttpObject(){
var xmlHttpRequest;
//不同瀏覽器獲取對(duì)象xmlHttpRequest方法不一樣
if(window.ActiveXObject){
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest=new XMLHttpRequest();
}
return xmlHttpRequest;
}
//驗(yàn)證用戶名是否存在
var myXmlHttpRequest="";//因?yàn)閏huli也用到了,所以要定義為全局變量
//創(chuàng)建方法(2號(hào)線 http請(qǐng)求)
function checkName(){
//創(chuàng)建對(duì)象
myXmlHttpRequest=getXmlHttpObject();
//判斷是否創(chuàng)建ok
if(myXmlHttpRequest){
//通過myXmlHttpRequest對(duì)象發(fā)送請(qǐng)求到服務(wù)器的某個(gè)頁面
var url="./registerPro1.php";
//要發(fā)送的數(shù)據(jù)
var data="username="+$('username').value;
//打開請(qǐng)求
myXmlHttpRequest.open("post",url,true);//ture表示使用異步機(jī)制
//POST方法
myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//指定回調(diào)函數(shù),chuli是函數(shù)名(registerPro里的數(shù)據(jù)返回給chuli函數(shù))
myXmlHttpRequest.onreadystatechange=chuli;
//開始發(fā)送數(shù)據(jù),如果是get請(qǐng)求則填入null即可,如果是post請(qǐng)求則填入實(shí)際的數(shù)據(jù)
myXmlHttpRequest.send(data);
}
}
//回調(diào)函數(shù)(4號(hào)線)
function chuli(){
//取出從registerPro.php頁面返回的數(shù)據(jù)(4表示完成,200表示成功)
if(myXmlHttpRequest.readyState==4){
if(myXmlHttpRequest.status==200){
//①、取出值,根據(jù)返回信息的格式定 text(html)
//$('result').value=myXmlHttpRequest.responseText;
//②、取出xml格式數(shù)據(jù)(解析)
//獲取mes節(jié)點(diǎn)、這里的mes返回的是節(jié)點(diǎn)列表(不知道有幾個(gè)mes)
//var mes=myXmlHttpRequest.responseXML.getElementsByTagName("mes");
//取出mes節(jié)點(diǎn)值
//mes[0]->表示取出第一個(gè)mes節(jié)點(diǎn)
//mes[0].childNodes[0]->表示取出mes節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn)
//var mes_val=mes[0].childNodes[0].nodeValue;
//$("result").value=mes_val;
//③、json格式
//var mes=myXmlHttpRequest.responseText;
//使用eval函數(shù),將mes字串轉(zhuǎn)為對(duì)象
//var mes_obj=eval("("+mes+")");
//$('result').value=mes_obj.res;
//③+、json格式擴(kuò)展
var mes=myXmlHttpRequest.responseText;
var mes_obj=eval("("+mes+")");
$('result').value=mes_obj[0].res;
}
}
}
//封裝一個(gè)函數(shù),通過id號(hào)獲取對(duì)象
function $(id){
return document.getElementById(id);
}
</script>
<br/>
<strong style="color:red">發(fā)表留言</strong>
<form action="#" method="POST" name="frm">
<table cellpadding="0" cellspacing="0" >
<tr>
<td >留言標(biāo)題:</td>
<td><input type="text" name="title" autocomplete="off"/></td>
</tr>
<tr>
<td>網(wǎng)名:</td>
<td>
<input id="username" onkeyup="checkName();" type="text" name="username" autocomplete="off"/>
<td><input id="result" type="text" style="width:110px;font-size: 12px;border-width:0;" ></td>
</td>
</tr>
<tr>
<td>留言內(nèi)容:</td>
<td><textarea name="content" cols="26" rows="5" autocomplete="off"/ onclick="showNotice(this)"></textarea></td>
</tr>
<tr>
<td></td>
<td><input class="btn" type="submit" name="submit" value="提交"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
registerPro1.php
<?php
//將數(shù)據(jù)(text格式,xml格式,json格式)返回到ajax引擎(3號(hào)線 http響應(yīng) )
//header("Content-Type: text/xml; charset=utf-8"); //告訴瀏覽器,返回的是xml格式
header("Content-Type: text/html; charset=utf-8"); //告訴瀏覽器,返回的是text/json格式
$username = $_POST["username"];
//①
// if($username=="abc"){
// echo '網(wǎng)名不可用';
// }else{
// echo '網(wǎng)名可用';
// }
//②
// $info="";
// if($username=="abc"){
// $info.="<res><mes>網(wǎng)名不可用</mes></res>";
// }else{
// $info.="<res><mes>網(wǎng)名可用</mes></res>";
// }
// echo $info;
//③
// $info="";
// if($username=="abc"){
// //這里的$info返回的是一個(gè)字串
// $info.='{"res":"不可用","id":"123","age":"5"}';
// }else{
// $info.='{"res":"可用","id":"3","age":"1"}';
// }
// echo $info;
//③+
$info="";
if($username=="abc"){
//這里的$info返回的是一個(gè)字串
$info.='[{"res":"不可用","id":"123","age":"5"},{"res":"abc不可用","id":"3","age":"0"}]';
}else{
$info.='[{"res":"可用","id":"1","age":"15"},{"res":"可用","id":"83","age":"9"}]';
}
echo $info;
?>
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Ajax檢測(cè)用戶名是否被占用的完整實(shí)例
- JQuery Ajax如何實(shí)現(xiàn)注冊(cè)檢測(cè)用戶名
- PHP+Ajax實(shí)現(xiàn)的檢測(cè)用戶名功能簡(jiǎn)單示例
- AJAX應(yīng)用實(shí)例之檢測(cè)用戶名是否唯一(實(shí)例代碼)
- 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è))
- PHP+AJAX實(shí)現(xiàn)無刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
- ajax 檢測(cè)用戶名是否被占用
- AJAX檢測(cè)用戶名是否存在的方法
相關(guān)文章
AJAX應(yīng)用之注冊(cè)用戶即時(shí)檢測(cè)
AJAX應(yīng)用之注冊(cè)用戶即時(shí)檢測(cè)...2006-07-07
jQuery Validator驗(yàn)證Ajax提交表單的方法和Ajax傳參的方法
這篇文章主要介紹了jQuery Validator驗(yàn)證Ajax提交表單的方法和Ajax傳參的方法,在文中還給大家提到了jquery .ajax提交表單的寫法,具體實(shí)例代碼大家參考下本文2017-08-08
Ajax傳Jsond Mvc后臺(tái)接收實(shí)例
Ajax傳Jsond Mvc后臺(tái)接收,前后臺(tái)實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-06-06
ajax JSONP請(qǐng)求處理回調(diào)函數(shù)jsonpCallback區(qū)分大小寫
使用ajax進(jìn)行 JSONP跨域請(qǐng)求,因?yàn)楸徽?qǐng)求的對(duì)方的回調(diào)函數(shù)名稱是無法修改,想到設(shè)置AJAX 的JSONP參數(shù)。但是發(fā)現(xiàn)根本不起作用。最后偶然發(fā)現(xiàn) jsonpcallback是區(qū)分大小寫的2013-09-09
js實(shí)現(xiàn)簡(jiǎn)單實(shí)用的AJAX完整實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)簡(jiǎn)單實(shí)用的AJAX的方法,以完整實(shí)例形式分析了ajax無刷新調(diào)用的具體實(shí)現(xiàn)步驟與相關(guān)技巧,代碼備有詳盡的注釋便于理解,需要的朋友可以參考下2016-02-02
PHP+AJAX無刷新實(shí)現(xiàn)返回天氣預(yù)報(bào)數(shù)據(jù)
天氣數(shù)據(jù)是通過采集中國(guó)氣象網(wǎng)站的。本來中國(guó)天氣網(wǎng)站也給出了數(shù)據(jù)的API接口,接下來為大家介紹下用php來寫一個(gè)天氣預(yù)報(bào)的模塊,感興趣的朋友可以參考下2013-07-07

