您的位置:首页 > 编程语言 > PHP开发

php+ajax 注册验证用户名是否存在实例

2010-12-20 17:53 661 查看
register.html

<script>
//创建ajax
function creatAjax()
{
var HttpRequest=false;
try {
HttpRequest=new XMLHttpRequest();
} catch(e) {
var arrXMLHTTP=["Msxml3.XMLHTTP","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0;i<arrXMLHTTP.length;i++) {
try {
HttpRequest=new ActiveXObject(arrXMLHTTP[i]);
} catch(e) {}
if(HttpRequest) break;
}
}
return HttpRequest;
}

//使用ajax
function useAjax(username){
var ajax = creatAjax();
strUrl = “register.php”
ajax.open(”POST”,strUrl,false);
ajax.setRequestHeader(”Content-Type”,”application/x-www-form-urlencoded”);
strPost=”username=”+username;
ajax.send(strPost);
var str = ajax.responseText;
if(str==1){
document.getElementById(’ajax’).innerHTML=’此会员名已被注册,请更换会员名!’;
}else{
document.getElementById(’ajax’).innerHTML=’此会员名可以注册!’;
} //alert(str);
return false;
}

</script>
<input type=”text” onblur=”useAjax(this.value)”><span id=”ajax”></span>

register.php

<?php
mysql_connect(’localhost’,'root’,”);
mysql_select_db(’ecshop_utf8′);
$strSql = mysql_query(”select user_name from ecs_users where user_name =’”.$_POST["username"].”‘”);
$line = mysql_fetch_array($strSql,MYSQL_ASSOC);

if($line["user_name"]){
echo “1″;
exit;
}else{
echo “0″;
exit;

}
?>
checkuserreg.html

<html>
<head>
<script language=”javascript”>// JavaScript Document
var http_request=false;
function send_request(url){//初始化,指定处理函数,发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){//设置MIME类别
http_request.overrideMimeType(”text/xml”);
}
}
else if(window.ActiveXObject){//IE浏览器
try{
http_request=new ActiveXObject(”Msxml2.XMLHttp”);
}catch(e){
try{
http_request=new ActiveXobject(”Microsoft.XMLHttp”);
}catch(e){}
}
}
if(!http_request){//异常,创建对象实例失败
window.alert(”创建XMLHttp对象失败!”);
return false;
}
}
//处理返回信息的函数
function processrequest(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
document.getElementById(’re’).innerHTML=http_request.responseText;
}
else{//页面不正常
alert(”您所请求的页面不正常!”);
}
}
}
function processrequest2(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
document.getElementById(’wrong’).innerHTML=http_request.responseText;
window.location.href=”http://www.baidu.com“; //注册成功后跳转到百度,可以自定义
}
else{//页面不正常
alert(”您所请求的页面不正常!”);
}
}
}
function check(){
var f=document.form1;
var uname=f.username.value;
if(uname == “”){
document.getElementById(’re’).innerHTML=’用户名不能为空!’;
f.username.focus();
return false;
}
else{
document.getElementById(’re’).innerHTML=’正在读取数据…’;
var username=document.form1.username.value;
var queryString=”username=”+username;
send_request();
http_request.open(”POST”,”checkuserreg.php”,true);
http_request.onreadystatechange=processrequest;
http_request.setRequestHeader(”Content-Type”,”application/x-www-form-urlencoded;”);
//确定发送请求方式,URL,及是否同步执行下段代码
//http_request.open(”GET”,url,true);
http_request.send(queryString);
}
}
</script>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″></head>
<body>
<form action=”" method=”post” name=”form1″>
<table width=”67%” height=”144″ border=”1″ align=”center” cellpadding=”1″ cellspacing=”1″>
<tr>
<td width=”107″ height=”49″ align=”left” bgcolor=”#FFFFFF”>用户名称:</td>
<td width=”231″ align=”left” bgcolor=”#FFFFFF”>
<input name=”username” type=”text” id=”username” >
<input type=”button” value=”检测” onClick=”check();” /><br /><span id=”re” style=”color:#FF0000; font-size:14px; text-align:center;”></span></td>
<td width=”477″ align=”left” bgcolor=”#FFFFFF” id=”check”> 4-16个字符,英文小写、汉字、数字、最好不要全部是数字。</td>
</tr>

</table>
</form>
</body>
</html>

checkuserreg.php

<?php
header(’Content-Type:text/html;charset=GB2312′);//避免输出乱码
$dbhost = “localhost”;
$dbuser = “root”;
$dbpassword = “”;
$dbname = “ecshop_utf8″;
mysql_connect($dbhost,$dbuser,$dbpassword) or die(”error!”);
mysql_query(”set names ‘gbk’”);
mysql_select_db(’ecshop_utf8′);

$username=trim($_POST['username']);//获取注册名
$sql=”select user_name from ecs_users where user_name=’$username’”;//查询会员名
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$rows=mysql_fetch_array($result);
if($num<>0){
echo “此会员名已被注册,请更换会员名!”;
}
else{
echo “此会员名可以注册!”;
}
mysql_close();//关闭数据库连接
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: