亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

AJAX實(shí)現(xiàn)無(wú)刷新檢測(cè)用戶(hù)名功能

 更新時(shí)間:2017年06月09日 10:46:43   作者:壞人站住  
這篇文章主要為大家詳細(xì)介紹了AJAX實(shí)現(xiàn)無(wú)刷新用戶(hù)名檢測(cè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

先來(lái)看看原理圖

register.php

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title>ajax無(wú)刷新檢測(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)證用戶(hù)名是否存在
    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){
      //通過(guò)myXmlHttpRequest對(duì)象發(fā)送請(qǐng)求到服務(wù)器的某個(gè)頁(yè)面 
      var url="./registerPro1.php";
      //要發(fā)送的數(shù)據(jù)
      var data="username="+$('username').value;
      //打開(kāi)請(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;
      //開(kāi)始發(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頁(yè)面返回的數(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ù),通過(guò)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>留言?xún)?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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論