ajax實現檢測當前註冊用戶是否存在

請求頁面:ajax.html
php

<!doctype html>html

<html>ajax

<head>數據庫

<meta charset="utf-8">ide

</head>spa

<body>htm

姓名:<input type="text" id="uname"/ ><span id="msg"></span><br/>對象

</body>事件

</html>ip

<script>

function $(id){

   return document.getElementById(id);   //根據指定的 id 屬性值獲得對象

}

$('uname').onkeyup=function(){  

//在鍵盤按鍵被鬆開時觸發事件

   var n = $('uname').value;   //將對象的值賦給新的變量

var x = new XMLHttpRequest();   //建立ajax請求對象

x.open('GET','ajax.php?n='+n,true);  //設置傳送方式和路徑

x.onreadystatechange=function(){          //

   if(x.readyState==4 && x.status==200){

   $('msg').innerHTML = x.responseText;  //在ID爲msg的標記中載入獲取的數據

}

}

x.send();//發送數據

}

</script>


響應檢測頁面:

<?php

if(isset($_GET['n'])&&$_GET['n']!=""){

   $n = $_GET['n'];

       if($n=='admin'){   //用admin表明數據庫查詢到的值

       echo '<font color=red>已經存在</font>';

   }else{

   echo '<font color=green>不存在,可使用</font>';

}

}

相關文章
相關標籤/搜索