首页 > php开发 > php ajax注册验证用户名是否存在代码

php ajax注册验证用户名是否存在代码

这是注册程序是一款当用户输入完用户名是,就会自动去数据库中查询用户要注册的用户名是否己经被注册了,如果是返回提示否则提示可以注册。

conn.php文件

<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<script > 
var xmlhttp
function showhint(str) {
    if (str.length == 0) {
        document.getelementbyid("txthint").innerhtml = ""
            return
    }
    xmlhttp = getxmlhttpobject()
        if (xmlhttp == null) {
            alert("browser does not support http request")
            return
        }
        xmlhttp.onreadystatechange = statechanged
        var geturl = "conn.php?q=" + str
        //sid是增加一个随机数 防止页面启用缓存技术&middot;
        geturl = geturl + "&sid=" + math.random()
        geturl = encodeuri(geturl);
    geturl = encodeuri(geturl);
    xmlhttp.open("get", geturl, true)
    xmlhttp.send(null)
}
function statechanged() {
    if (xmlhttp.readystate == 4 || xmlhttp.readystate == "complete") {
        document.getelementbyid("txthint").innerhtml = xmlhttp.responsetext
    }
}
function getxmlhttpobject() {
    var xmlhttp = null;
    try {
        // firefox, opera 8.0+, safari
        xmlhttp = new xmlhttprequest();
    } catch (e) {
        // internet explorer
        try {
            xmlhttp = new activexobject("msxml2.xmlhttp");
        } catch (e) {
            xmlhttp = new activexobject("microsoft.xmlhttp");
        }
    }
    return xmlhttp;
}
</script>  
</head> 
<body bgcolor="#999999"> 
<center> 
<form>  
<table> 
 <tr> 
  <td>用户名:</td> 
  <td><input type="text" id="txt1" onkeyup="showhint(this.value)"></td> 
 </tr> 
 <tr align="center"> 
  <td colspan="2"><span id="txthint"></span></td> 
 </tr> 
</table> 
</form> 
</center> 
</body> 
</html>
<?php
$q = $_get["q"];
$q = urldecode($q);
if (strlen($q) > 0) {
    $conn = @mysql_connect("localhost", "root", "1010") or die("mysql连接错误");
    mysql_select_db("xin", $conn);
    mysql_query("set names 'utf8'");
    $sql = "select username from message where username = '$q'";
    $query = mysql_query($sql);
    @$row = mysql_fetch_array($query);
    if (!emptyempty($row['username'])) {
        $response = "<font color=red>已经被注册!</font>";
    } else {
        $response = "<font color=blue>恭喜!可以注册!</font>";
    }
    echo $response;
}
?>

数据库

drop database if exists `xin`; 
create database `xin` /*!40100 default character set utf8 */; 
use `xin`; 
 
create table `message` ( 
  `id` int(11) not null auto_increment, 
  `username` varchar(20) default null, 
  primary key  (`id`) 
) engine=innodb auto_increment=2 default charset=utf8;


本文地址:http://www.phprm.com/develop/fs933.html

转载随意,但请附上文章地址:-)

标签:ajax 注册 验证

相关文章

发表留言