首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

最简单的用户验证功能 Ajax菜鸟 不知道以上代码错哪了 求大神解救!

2012-12-29 
最简单的用户验证功能 Ajax初学者 不知道以下代码哪里错了 求大神解救!!ajax.js文件// JavaScript Documen

最简单的用户验证功能 Ajax初学者 不知道以下代码哪里错了 求大神解救!!
ajax.js文件


// JavaScript Document
//var xmlHttp;   
    function createXMLHttpRequest()   
    {   
          if(window.XMLHttpRequest)   
        {   
             var xmlHttp = new XMLHttpRequest();//mozilla浏览器   
        }   
        else if(window.ActiveXObject)   
        {   
            try  
            {   
            var xmlHttp = new ActiveX0bject("Msxml2.XMLHTTP");//IE老版本   
            }   
            catch(e)   
            {}   
            try  
            {   
               var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE新版本   
            }   
            catch(e)   
            {}   
            if(!xmlHttp)   
            {   
                window.alert("不能创建XMLHttpRequest对象实例");   
                return false;   
            }   
else
  return xmlHttp; 
        }   
    }   
       
       
   function startRequest(username)   
        {   
              
            createXMLHttpRequest(); 
               
              xmlHttp.open("GET","checkname.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){   
                    //alert("失败");   
                    document.getElementById("ckuser").innerHTML = '此用户名以被人注册';   
                }   
                else if(xmlHttp.responseText == false)   
                {   
                    //alert("成功");   
                   document.getElementById("ckuser").innerHTML = '检测通过';   
                }   
            }   
else alert("服务器端错误");
        }   
    }   


checkname.php文件

<?php 
include("conn.php");
$username = $_GET["name"];  
$sql="select id from personinfo where UserName='".$username."'";
$query=mysql_query($sql,$conn);
$result=mysql_num_rows($query);
  if($result!=0)   
        {   
             echo true;   
        }
  else  
        {   
             echo false;  
  }   

?>

index.php文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax用户验证</title>
<link rel="stylesheet" href="style/sty.css" type="text/css" />
<script src="ajax.js" type="text/javascript" ></script>


</head>

<body>
<div class="inp_name">
 <form>
  <div class="inp_name_l">请输入姓名
    <input type="text" name="username" id="username" value="" onBlur="startRequest(document.getElementById('username').value);" /> 
  </div>
 </form>  
  <div class="inp_name_r" id="ckuser"> </div>
</div>
</body>
</html>


[解决办法]
    // JavaScript Document
    var xmlHttp;
    function createXMLHttpRequest() {
        if (window.XMLHttpRequest) {
            var xmlHttp = new XMLHttpRequest(); //mozilla浏览器   
        }
        else if (window.ActiveXObject) {
            try {
                var xmlHttp = new ActiveX0bject("Msxml2.XMLHTTP"); //IE老版本   
            }
            catch (e)
            { }
            try {
                var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE新版本   
            }
            catch (e)
            { }
        }
        if (!xmlHttp) {
            window.alert("不能创建XMLHttpRequest对象实例");
            return false;
        }
        else
            return xmlHttp;
        ////}///////花括号放错地方
    }


    function startRequest(username) {

        xmlHttp = createXMLHttpRequest(); //createXMLHttpRequest申明的是局部变量,这里访问不到xmlHttp,要给xmlHttp变量赋值才行

        xmlHttp.open("GET", "checkname.php?name=" + username+'&_dc='+new Date().getTime(), true);//加时间戳防止IE缓存
        xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.send(null);
    }


    function handleStateChange() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {


                //alert("来自服务器的响应:" + xmlHttp.responseText);   
                if (xmlHttp.responseText == 'true') {///////responseText是字符串,不能和boolean变量直接比较
                    //alert("失败");   
                    document.getElementById("ckuser").innerHTML = '此用户名以被人注册';
                }
                else if (xmlHttp.responseText == 'false') {///////responseText是字符串,不能和boolean变量直接比较
                    //alert("成功");   
                    document.getElementById("ckuser").innerHTML = '检测通过';
                }
            }
            else alert("服务器端错误");
        }
    }

   
[解决办法]
xmlHttp变量作用域的问题,自己检查下。

热点排行