Home >Backend Development >PHP Tutorial >Code for php+ajax to dynamically verify whether the username has been registered

Code for php+ajax to dynamically verify whether the username has been registered

WBOY
WBOYOriginal
2016-07-25 08:58:191157browse
本文介绍下,php与ajax结合,实现验证用户名是否已注册的代码,有需要的朋友,可以参考下。

1,用户注册页 Login.html

<html> 
<head> 
<title>用户注册_bbs.it-home.org</title> 
<script type="text/javascript"> 
var xmlHttp; 
function createXMLHttpRequest() 
{ 
    //在IE下创建XMLHttpRequest对象 
    try 
    { 
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
    } 
    catch(e) 
    {        
    try 
    { 
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    }  
    catch(oc) 
    { 
        xmlHttp = null; 
    } 
    } 
    //在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象 
    if(!xmlHttp && typeof XMLHttpRequest != "undefined")  
    { 
        xmlHttp = new XMLHttpRequest(); 
    } 
    return xmlHttp; 
} 
//方式请求 
function startRequest() 
{ 
    //获取用户输入的信息 
    var UserID = document.getElementById("UserID").value; 
    //输入的用户名是否为空 
    if(UserID!="") 
    { 
        //创建XMLHttpRequest对象 
        createXMLHttpRequest(); 
        var url = "Login.php?UserID="+encodeURI(UserID); //指定url 
        xmlHttp.open("GET",url,true); 
        xmlHttp.onreadystatechange = handleStateChange;  //指定回调函数 
        xmlHttp.send(null);   
    } 
    else 
    { 
        alert("请填写您的用户名!"); 
    } 
} 
function handleStateChange() 
{ 
    if(xmlHttp.readyState<4) 
    { 
        span.innerHTML="正在读取数据..."; 
    } 
 
    if(xmlHttp.readyState==4) 
    { 
        if(xmlHttp.status == 200)  
        { 
            span.innerHTML=xmlHttp.responseText; 
        } 
        else{ 
            alert("错误,请求页面异常!"); 
        } 
    } 
}  
</script> 
</head> 
<body> 
<form action="#"> 
<center> 
<h3>Ajax+php验证用户名是否可用</h3> 
<table> 
<tr> 
<td>用户名:  <input type="text" name="UserID" id="UserID" onchange="startRequest()" /> 
<span id="span"></span></td> 
</tr> 
<tr> 
<td>密  码:   <input type="text" name="Pwd" id="Pwd"/></td> 
</tr> 
<tr> 
<td> 
<br/>
<input type="button" name="Reg" id="Reg" value="注册"/> 
<input type="button" name="Log" id="Log" value="登录"/> 
</td> 
</tr> 
</table> 
</center> 
<form>  
</body>
</html>

2,注册检测页 Login.php

<?php 
//设置PHP的显示语言 
header('Content-Type:text/html;charset=UTF-8'); 
//让程序先等待一秒 
sleep(1);  
//获取输入的用户名 
$tmp=$_GET["UserID"]; 
$db_host="localhost"; 
$db_name="canphp"; 
$db_username="root"; 
$db_password="rootpass"; 
$link = mysql_connect($db_host, $db_username,$db_password ) or  die ('Can\'t connet db!' . mysql_error()); 
mysql_select_db($db_name, $link) or die ('Can\'t select db : ' . mysql_error()); 
$query="select * from cp_user where username='$tmp'"; 
$result = mysql_query($query) or  die ($query.mysql_error()); 
$rows=mysql_num_rows($result); 
if($rows>0) 
{ //存在记录
   echo "用户名已存在";  
}else{ 
  echo("可以注册!");  
}  
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn