PHP (20140507)

通过Ajax到数据库去验证是否存在这个用户:

PHP代码:

 1 <?php
 2 if(isset($_GET['user'])){
 3     $user = $_GET['user'];
 4     include("util.php");
 5     $sql = "select count(*) from `username` where sname = '$user'";
 6     $result = mysql_query($sql);
 7     $row = mysql_fetch_row($result);
 9     if($row[0]){
10         echo "该用户已经被注册!";
11     }else {
12         echo "该用户还没有被使用,可以注册!";
13     }
14 }
15 
16 ?>

js代码:

 1 var xmlHttp;
 2 function getXmlHttp(){
 3     if(window.ActiveXObject){
 4         xmlHttp = new ActiveXObject("MICROSOFT.XMLHTTP");
 5     }else if(window.XMLHttpRequest){
 6         xmlHttp = new XMLHttpRequest();
 7     }
 8 }
 9 function sendParam(url){
10     getXmlHttp();
11     xmlHttp.open("GET","register.php?user="+url,true);
12     xmlHttp.onreadystatechange = getTxt;
13     xmlHttp.send(null);
14 }
15 function getTxt(){
16     if(xmlHttp.readyState == 4){
17         if(xmlHttp.status == 200){
18             var sp = document.getElementById("tishi");
19             sp.innerHTML = xmlHttp.responseText;
20         }
21     }
22 }

HTML代码:

1 <form  method="post" action="register.php">
2             用户: <input class="inp" onblur="sendParam(this.value)" type="text" name="user">
3             <span style="color: #ff0000;font-size: 16px" id="tishi"></span>  <br> <br>
4             密码:<input class="inp" type="password" name="pwd"><span></span>
5             <input id="ce" type="submit" name="submit" value="注册">
6         </form>
原文地址:https://www.cnblogs.com/sunshine-c/p/3714143.html