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

在用户注册检测用户名是否存在我们要提供告诉用户你要注册的用户名是否可用,那么我们就得利用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>

                       


本文地址:

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.