這篇文章將為大家詳細(xì)講解有關(guān)AJAX如何實現(xiàn)無刷新檢測用戶名功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司服務(wù)項目包括定西網(wǎng)站建設(shè)、定西網(wǎng)站制作、定西網(wǎng)頁制作以及定西網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,定西網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到定西省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!先來看看原理圖

register.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>ajax無刷新檢測</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號線)
function getXmlHttpObject(){
var xmlHttpRequest;
//不同瀏覽器獲取對象xmlHttpRequest方法不一樣
if(window.ActiveXObject){
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest=new XMLHttpRequest();
}
return xmlHttpRequest;
}
//驗證用戶名是否存在
var myXmlHttpRequest="";//因為chuli也用到了,所以要定義為全局變量
//創(chuàng)建方法(2號線 http請求)
function checkName(){
//創(chuàng)建對象
myXmlHttpRequest=getXmlHttpObject();
//判斷是否創(chuàng)建ok
if(myXmlHttpRequest){
//通過myXmlHttpRequest對象發(fā)送請求到服務(wù)器的某個頁面
var url="./registerPro1.php";
//要發(fā)送的數(shù)據(jù)
var data="username="+$('username').value;
//打開請求
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請求則填入null即可,如果是post請求則填入實際的數(shù)據(jù)
myXmlHttpRequest.send(data);
}
}
//回調(diào)函數(shù)(4號線)
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é)點、這里的mes返回的是節(jié)點列表(不知道有幾個mes)
//var mes=myXmlHttpRequest.responseXML.getElementsByTagName("mes");
//取出mes節(jié)點值
//mes[0]->表示取出第一個mes節(jié)點
//mes[0].childNodes[0]->表示取出mes節(jié)點的第一個子節(jié)點
//var mes_val=mes[0].childNodes[0].nodeValue;
//$("result").value=mes_val;
//③、json格式
//var mes=myXmlHttpRequest.responseText;
//使用eval函數(shù),將mes字串轉(zhuǎn)為對象
//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;
}
}
}
//封裝一個函數(shù),通過id號獲取對象
function $(id){
return document.getElementById(id);
}
</script>
<br/>
<strong >發(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" ></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號線 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返回的是一個字串
// $info.='{"res":"不可用","id":"123","age":"5"}';
// }else{
// $info.='{"res":"可用","id":"3","age":"1"}';
// }
// echo $info;
//③+
$info="";
if($username=="abc"){
//這里的$info返回的是一個字串
$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;
?>效果圖:

ajax是一種在無需重新加載整個網(wǎng)頁的情況下,能夠更新部分網(wǎng)頁的技術(shù),可以通過在后臺與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,使網(wǎng)頁實現(xiàn)異步更新。
關(guān)于“AJAX如何實現(xiàn)無刷新檢測用戶名功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
當(dāng)前標(biāo)題:AJAX如何實現(xiàn)無刷新檢測用戶名功能-創(chuàng)新互聯(lián)
分享鏈接:http://www.yijiale78.com/article32/ceijsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站制作、電子商務(wù)、用戶體驗、品牌網(wǎng)站制作、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容