AJAX實現(xiàn)無刷新檢測用戶名功能
更新時間:2017年06月09日 10:46:43 作者:壞人站住
這篇文章主要為大家詳細介紹了AJAX實現(xiàn)無刷新用戶名檢測功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
先來看看原理圖
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表示使用異步機制 //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格式擴展 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 style="color:red">發(fā)表留言</strong> <form action="#" method="POST" name="frm"> <table cellpadding="0" cellspacing="0" > <tr> <td >留言標題:</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號線 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; ?>
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 利用Ajax檢測用戶名是否被占用的完整實例
- JQuery Ajax如何實現(xiàn)注冊檢測用戶名
- PHP+Ajax實現(xiàn)的檢測用戶名功能簡單示例
- AJAX應(yīng)用實例之檢測用戶名是否唯一(實例代碼)
- jQuery+Ajax實現(xiàn)用戶名重名實時檢測
- 使用Ajax實時檢測"用戶名、郵箱等"是否已經(jīng)存在
- PHP+Ajax檢測用戶名或郵件注冊時是否已經(jīng)存在實例教程
- Asp.net下利用Jquery Ajax實現(xiàn)用戶注冊檢測(驗證用戶名是否存)
- jquery ajax 檢測用戶注冊時用戶名是否存在
- PHP+AJAX實現(xiàn)無刷新注冊(帶用戶名實時檢測)
- PHP+AJAX實現(xiàn)無刷新注冊(帶用戶名實時檢測)
- ajax 檢測用戶名是否被占用
- AJAX檢測用戶名是否存在的方法
相關(guān)文章
jQuery Validator驗證Ajax提交表單的方法和Ajax傳參的方法
這篇文章主要介紹了jQuery Validator驗證Ajax提交表單的方法和Ajax傳參的方法,在文中還給大家提到了jquery .ajax提交表單的寫法,具體實例代碼大家參考下本文2017-08-08ajax JSONP請求處理回調(diào)函數(shù)jsonpCallback區(qū)分大小寫
使用ajax進行 JSONP跨域請求,因為被請求的對方的回調(diào)函數(shù)名稱是無法修改,想到設(shè)置AJAX 的JSONP參數(shù)。但是發(fā)現(xiàn)根本不起作用。最后偶然發(fā)現(xiàn) jsonpcallback是區(qū)分大小寫的2013-09-09PHP+AJAX無刷新實現(xiàn)返回天氣預(yù)報數(shù)據(jù)
天氣數(shù)據(jù)是通過采集中國氣象網(wǎng)站的。本來中國天氣網(wǎng)站也給出了數(shù)據(jù)的API接口,接下來為大家介紹下用php來寫一個天氣預(yù)報的模塊,感興趣的朋友可以參考下2013-07-07