教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php+ajax动态验证用户名是否已注册的代码

php+ajax动态验证用户名是否已注册的代码

发布时间:2016-07-05   编辑:jiaochengji.com
本文介绍下,php与ajax结合,实现验证用户名是否已注册的代码,有需要的朋友,可以参考下。

1,用户注册页 Login.html

<html> 
<head> 
<title>用户注册_www.jbxue.com</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>密&nbsp;&nbsp;码:   <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("可以注册!");  
}  
?>

您可能感兴趣的文章:
php+ajax动态验证用户名是否已注册的代码
php+ajax用户注册验证用户是否存在的完整实例
PHP开发常见功能实现流程
jquery ajax 检测用户注册时用户名是否存在
php验证码大全(实例分享)
js能用php代替吗
Asp.net下利用Jquery Ajax实现用户注册检测(验证用户名是否存)
php实现用户注册的代码示例
php与mysql开发用户登录验证的实例代码
API 交互中怎么做好图片验证码

[关闭]
~ ~