首页 > php代码 > php ajax用户注册检测代码

php ajax用户注册检测代码

php ajax用户注册检测代码

index.php 复制PHP内容到剪贴板 PHP代码:

<tr>
  <td width="25%">&nbsp;&nbsp;用 户 名<font color="red">*</font><br/></td>
  <td>
    <input size="25" name="username" type="text" value=""
    onBlur="startRequest(document.getElementById('username').value);" />
  </td>
  <td id="ckuser"></td>
</tr>

 

js:ajax.js 复制PHP内容到剪贴板 var xmlHttp;

function createXMLHttpRequest() {
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest(); //mozilla浏览器
    } else if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveX0bject("Msxml2.XMLHTTP"); //IE老版本
        } catch (e) {}
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE新版本
        } catch (e) {}
        if (!xmlHttp) {
            window.alert("不能创建XMLHttpRequest对象实例");
            return false;
        }
    }
}
php ajax用户注册检测代码
function startRequest(username) {
    createXMLHttpRequest(); //特编
    xmlHttp.open("GET", "ckuser.php?name=" + username, true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.send(null);
}
function handleStateChange() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            //alert("来自服务器的响应:" + xmlHttp.responseText);
            if (xmlHttp.responseText == "true") {
                document.getElementById("ckuser").innerHTML = '此用户名以被人注册';
            } else if (xmlHttp.responseText == "false") {
                document.getElementById("ckuser").innerHTML = '检测通过';
            }
        }
    }
}

ckuser.php   php ajax用户注册检测代码

PHP代码:

<?php
//require_once "globals.php";
require_once "config.inc.php";
//require_once 'common/common.php';
$username = $_GET["name"];
$con = @mysql_connect("$dbserver","$dbuser","$dbpass")or die("无法连接到数据库!");
mysql_query("set names gbk");
mysql_select_db("$dbname",$con)or die("无法选择数据库!"); 
$query="select id from hj_member where username='".$username."';";
$res=mysql_query($query);
if(mysql_num_rows($res)!=0)
{
    echo "true";
}else
{
    echo "false";
}


本文地址:http://www.phprm.com/code/4ca93121552960e0cefcdd5ce9f3f881.html

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

标签:none

发表留言