標籤:style blog class code java ext
通過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>