Ajax實現非同步使用者名稱驗證功能_AJAX相關

來源:互聯網
上載者:User

先看看布局比較簡單,效果圖如下

ajax功能:

    當使用者填寫好帳號切換到密碼框的時候,使用ajax驗證帳號的可用性。檢驗的方法如下:首先建立XMLHTTPRequest對象,然後將需要驗證的資訊(使用者名稱)發送到伺服器端進行驗證,最後根據伺服器返回狀態判斷使用者名稱是否可用。

function checkAccount(){var xmlhttp;var name = document.getElementById("account").value;if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp.open("GET","login.php?account="+name,true);xmlhttp.send(); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200)  document.getElementById("accountStatus").innerHTML=xmlhttp.responseText;}

運行結果

代碼實現

index.html

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Ajax登陸驗證</title><script type="text/javascript">function checkAccount(){var xmlhttp;var name = document.getElementById("account").value;if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp.open("GET","login.php?account="+name,true);xmlhttp.send(); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200)  document.getElementById("accountStatus").innerHTML=xmlhttp.responseText;}}</script></head><body><div id="content"><h2>使用Ajax實現非同步登陸驗證</h2><form>賬 號:<input type="text" id="account" autofocus required onblur="checkAccount()"></input><span id="accountStatus"></span><br><br>密 碼:<input type="password" id="password" required></input><span id="passwordStatus"></span><br><br><input type="submit" value="登陸"></input></form></div></body></html>

login.php

<?php  $con = mysqli_connect("localhost","root","GDHL007","sysu");   if(!empty($_GET['account'])){    $sql1 = 'select * from login where account = "'.$_GET['account'].'"';    //資料庫操作    $result1 = mysqli_query($con,$sql1);    if(mysqli_num_rows($result1)>0)      echo '<font style="color:#00FF00;">該使用者存在</font>';    else       echo '<font style="color:#FF0000;">該使用者不存在</font>';    mysqli_close($con);  }else    echo '<font style="color:#FF0000;">使用者名稱不可為空</font>'; ?>

以上就是本文的全部內容,希望對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.