一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php ajax用户注册检测代码

时间:2008-07-02 编辑:简简单单 来源:一聚教程网

php ajax用户注册检测代码

index.php 复制PHP内容到剪贴板 PHP代码:
                用 户 名*

             
               
             
             
           

 

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代码:
//
    //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";
            }


?>

热门栏目