Home > php教程 > php手册 > 用户注册检测用户名是否存在ajax+php代码

用户注册检测用户名是否存在ajax+php代码

WBOY
Release: 2016-05-25 16:42:43
Original
1264 people have browsed it

在用户注册检测用户名是否存在我们要提供告诉用户你要注册的用户名是否可用,那么我们就得利用ajax技术来实例,下面是一款ajax php当用户输入完用户名时提示用户是否可用用的代码:

<?php
$title = isset($_get[&#39;title&#39;]) ? $_get[&#39;title&#39;] : &#39;&#39;;
if ($title) {
    $sql = &#39;select id from filecontent where title=&#39;&#39;.$title.&#39;&#39;&#39;;
    $q = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($q)) {
        echo 1;
    } else {
        echo 0;
    }
} else {
    echo 0;
}
?>
<!doctype html> 
<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
<title> 用户注册检测用户名是否存在ajax + php代码</title> 
<script> 
//  用户注册检测用户名是否存在ajax + php代码 
 function createxmlhttprequest() { //创建xmlhttprequest对象
    if (window.activexobject) { //ie
        try {
            return new activexobject("microsoft.xmlhttp");
        } catch (e) {
            return;
        }
    } else if (window.xmlhttprequest) { //mozilla,firefox
        try {
            return new xmlhttprequest();
        } catch (e) {
            return;
        }
    }
 }
 function getrenews(value) { //主调函数
    var xmlhttp = createxmlhttprequest();
    var url = "t.php?action=check&title=" + value + "&mt=" + math.random(300000);
    if (value == "") {
        return false;
    }
    if (xmlhttp) {
        callback = getreadystatehandler(xmlhttp);
        xmlhttp.onreadystatechange = callback;
        xmlhttp.open("get", url, true);
        xmlhttp.send(null);
    }
 }
 //返回0代表用户名可用,否则提示己被注册。
 function getreadystatehandler(xmlhttp) { //服务器返回后处理函数
    return function () {
        if (xmlhttp.readystate == 4) {
            if (xmlhttp.status == 200) {
                if (xmlhttp.responsetext == 1) {
                    document.getelementbyid("checkid").innerhtml = "<font color=&#39;red&#39;>对不起,你输入的用户名己被注册!</font>";
                } else {
                    document.getelementbyid("checkid").innerhtml = "可以注册";
                }
            }
        }
    }
 }
</script> 
</head> 
 
<body> 
给input框增加onblur事件,当用户输入完用户名就检测用户名,并给出提示。 
输入用户名<input name="title" type="text" id="title" size="40" onblur="getrenews(this.value);"><span id="checkid"></span> 
</body> 
</html>
Copy after login

                       


本文地址:

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template