由于每當(dāng)用戶提交輸入,服務(wù)器就會返回新網(wǎng)頁,傳統(tǒng)的 web 應(yīng)用程序往往運(yùn)行緩慢,且越來越不友好。
通過 AJAX,web 應(yīng)用程序無需重載網(wǎng)頁,就可以發(fā)送并取回數(shù)據(jù)。完成這項工作,需要通過向服務(wù)器發(fā)送 HTTP 請求(在幕后),并通過當(dāng)服務(wù)器返回數(shù)據(jù)時使用 JavaScript 僅僅修改網(wǎng)頁的某部分。
一般使用 XML 作為接收服務(wù)器數(shù)據(jù)的格式,盡管可以使用任何格式,包括純文本。

無刷驗證新用戶名

自己最近看視頻自學(xué)ajax,想把一些實例分享給大家,第一個案列是無刷新驗證用戶名是否可用。

一、效果圖

1、用戶可用

2、用戶不可用

3、項目文件(register.php-注冊頁面 和process.php-判斷用戶名是否可用)

二、代碼

register.php-注冊頁面以及ajax發(fā)送請求

<!doctypehtml>
<htmllang="en">
<head>
  <meta charset="UTF-8">
  <title>注冊</title>
  <scripttype="text/javascript">
    //創(chuàng)建ajax引擎
    function getXmlHttpObject(){
      var xmlHttpRequest;
      //不同瀏覽器獲取對象XMLHttpRequest
      if(window.ActiveXObject){
        xmlHttpRequest=newActiveXObject("Microsoft.XMLHTTP");
      }
      else{
        xmlHttpRequest=newXMLHttpRequest();
      }
      return xmlHttpRequest;
    }
    var myXmlHttpRequest="";
    //驗證用戶名是否存在
    function checkName(){
      myXmlHttpRequest=getXmlHttpObject();
      //判斷xmlHttpRequest是否成功
      if(myXmlHttpRequest){
        //通過myXmlHttpRequest對象發(fā)送請求到服務(wù)器的某個頁面
        //第一個參數(shù)標(biāo)示請求的方式,‘get'、‘post'
        //第二個參數(shù)指定url,對那個頁面發(fā)送ajax請求(本質(zhì)仍然是http請求)
        /*XMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser,bstrPassword);
         */
        varurl="/Ajax/process.php?username="+$("username").value;
        //window.alert(url);
        myXmlHttpRequest.open("get",url,true);
        //window.alert('創(chuàng)建ajax引擎成功');
        //指定回調(diào)函數(shù),chuili是函數(shù)名
        myXmlHttpRequest.onreadystatechange=chuli;//調(diào)用
        //真的發(fā)送請求,如果是各塔請求則填入null即可
        //如果是post請求,則填入實際數(shù)據(jù)
        myXmlHttpRequest.send(null);
      }
      else
      {
//        window.alert('創(chuàng)建失敗');
      }
    }
    function chuli(){
      // window.alert("cuhli函數(shù)被調(diào)用"+myXmlHttpRequest.readyState);
      //我要取出從register.php返回的數(shù)據(jù)
      if(myXmlHttpRequest.readyState==4){
        //取出值,根據(jù)返回信息的數(shù)據(jù)格式
        //window.alert("服務(wù)器返回"+myXmlHttpRequest.responseText);
        $('myres').value=myXmlHttpRequest.responseText;
      }
    }
    function $(id){
      return document.getElementById(id);
    }
  </script>
</head>
<body>
<formaction="???" method="post">
  用戶名字:<inputtype="text" name="username1"onkeyup="checkName()" id="username">
  <input type="button"value="驗證用戶名">
  <input style="border-width:0;color: #e93b3d" type="text" id="myres">
  <br/>
  用戶密碼:<inputtype="password" name="password"><br>
  電子郵件:<inputtype="text" name="email"><br/>
  <input type="submit"value="用戶注冊">
</form>
</body>
</html>

process.php—判斷用戶名是否可用

<?php
  //接受數(shù)據(jù)
  $username=$_GET['username'];
//  echo "用戶名".$username;
  if($username=="李四"){
    echo "用戶名不可用";
  }
  else{
    echo"恭喜用戶名可用";
  }
?>

三、原理圖

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-62778877-8306;郵箱:fanjiao@west.cn。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明出處:西部數(shù)碼知識庫 » php Ajax無刷新驗證用戶名

登錄

找回密碼

注冊