function query(){
if (xmlHttp.readyState == 4 ){
if (xmlHttp.status==200)
{
if (xmlHttp.responseText=="true"){
document.getElementById("checkusername").innerText="该帐号已被占用";
//document.getElementById("checkusername").innerText=xmlHttp.responseText;
document.all.isadd.value="true";
}else {
document.getElementById("checkusername").innerText="该帐号可用";
document.all.isadd.value="false";
}
}
}
}
就算xmlHttp.responseText 返回的值是 true 上面的JS代码也不会执行  谁帮忙解决一下 疯了完整的代码
<input type="text" name="username" class="inputtext" size="25" onkeyup="usernamecheck()"> 
var xmlHttp;
function createAJAX(){
try{
xmlHttp = new XMLHttpRequest();
} catch(e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")
} catch(e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
alert("您的浏览器不支持AJAX!");
return;
}
}
}
}
function usernamecheck(){
str=document.userinfo.username.value;
var url="userquery.php?str="+str;
if(str.length!=0){
createAJAX();
xmlHttp.onreadystatechange=query;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}else{
document.getElementById("checkusername").innerText="";
}
}
function query(){
if (xmlHttp.readyState == 4 ){
if (xmlHttp.status==200)
{
if (xmlHttp.responseText=="true"){
document.getElementById("checkusername").innerText="该帐号已被占用";
//document.getElementById("checkusername").innerText=xmlHttp.responseText;
document.all.isadd.value="true";
}else {
document.getElementById("checkusername").innerText="该帐号可用";
document.all.isadd.value="false";
}
}
}
}
$conn=mysql_connect('localhost','root','root');
mysql_select_db('shop', $conn);
$str=$_GET["str"];
$sql="select * from userinfo where username='$str' ";
$result=mysql_query($sql);
if(mysql_num_rows($result)==0){
echo "false";
}else{
echo "true";
}
php的输出都正确定

解决方案 »

  1.   

    似乎没问题,楼主好好检查一下可以alert(xmlHttp.responseText)一下看看另外因为缓存原因,如果使用ie的话每改一次要清空缓存或另开页面测试才能看到改过的效果。
      

  2.   


    function query(){
        if (xmlHttp.readyState == 4 ){
            if (xmlHttp.status==200)
            {
                if (decodeURIComponent(xmlHttp.responseText)=="true"){
                    document.getElementById("checkusername").innerText="该帐号已被占用";
                    //document.getElementById("checkusername").innerText=xmlHttp.responseText;
                    document.all.isadd.value="true";
                }else {
                    document.getElementById("checkusername").innerText="该帐号可用";
                    document.all.isadd.value="false";
                }
            }
        }
    }
      

  3.   

    header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
    header("Pragma: no-cache"); /* 与HTTP1.0兼容 */
      

  4.   


    这是一种解决方案,另一种就是url后再加个变化的参数比如时间
      

  5.   

    楼主代码存在不少问题,这里给你重新写过~<script  type="text/javascript">
    var xmlHttp; function createAJAX()
    {
     xmlHttp=null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
      catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
      }
    return xmlHttp;
    }function usernamecheck(str) { 
    xmlHttp=createAJAX();
    if (xmlHttp==null)
      {
      alert ("您的浏览器不支持AJAX!");
      return;
      } 
    var url="userquery.php?str="+str;
    xmlHttp.onreadystatechange=query;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }function query(){
        if (xmlHttp.readyState == 4 ){
           if (xmlHttp.status==200){
                if (xmlHttp.responseText=="true"){
                    document.getElementById("checkusername").innerHTML="该帐号已被占用";
                    //document.getElementById("checkusername").innerText=xmlHttp.responseText;
                    document.all.isadd.value="true";
                }else {
                    document.getElementById("checkusername").innerHTML="该帐号可用";
                    document.all.isadd.value="false";
                }
            }
        }
    }</script>
    HTML 表单如下:<div>
    <input type="text" name="username" class="inputtext" size="25" onblur="usernamecheck(this.value);"> 
    <div><div id="checkusername"></div>PHP代码很简单就不写了,用你上面代码就可以了~~~
    有不明白的地方再跟贴说明,下班之前我会再关注下此贴。
      

  6.   

    试了各位的方法 还是不行啊...返回值是正确的  我在php页面输出sql 语句及结果 都是我所查询的值
    不知道怎么上传图片...要不然上传上来给各位看看
      

  7.   

    注意你php文件的文件编码,如果是utf-8+bom头就肯定错了
    你先alert(xmlHttp.responseText.length)看下返回串长度,如果返回true,应为4.
    还有一些js的书写要注意兼容问题,比如document.all在firefox下不支持,应该用标准的document.getElementById。。
      

  8.   

    返回值的长度是错的...我注册的那个页面编码GB2312 $conn=mysql_connect('localhost','root','root');
    mysql_select_db('shop', $conn);
    $str=$_GET["str"];
    $sql="select * from userinfo where username='$str' ";
    $result=mysql_query($sql);
    if(mysql_num_rows($result)==0){
        echo "false";
    }else{
        echo "true";
    }
     
    检查是这个 没有设编码但是为什么我用iframe 提交内容它输出的内容是正确的 而JS返回的长度又是错的
      

  9.   


    header('Content-type:text/html;charset=gb2312');
    $conn=mysql_connect('localhost','root','root');
     mysql_select_db('shop', $conn);
     $str=$_GET["str"]; 
    $sql="select * from userinfo where username='$str' "; 
    $result=mysql_query($sql); 
    if(mysql_num_rows($result)==0){ 
    echo "false"; 
    }else{
     echo "true"; 
    }
      

  10.   


    mb_internal_encoding("gb2312");  // 转变gb2312编码     
    mb_http_output("HTML-ENTITIES");     
    ob_start('mb_output_handler');  $conn=mysql_connect('localhost','root','root');
    mysql_select_db('shop', $conn);
    //注意数据库编码集应为GBK
    //mysql_query("SET NAMES 'GBK'");
    $str=$_GET["str"];
    $sql="select * from userinfo where username='$str' ";
    $result=mysql_query($sql);
    if(mysql_num_rows($result)==0){
        echo "false";
    }else{
        echo "true";
    }
      

  11.   

    楼猪啊,以后编码问题自己多注意!感觉帮你DEBUG真累啊~~~
      

  12.   

    就这几行内容,即使是gb2312也没关系,因为你echo的东西是ascii码0-127区的字符,不论什么编码都兼容这个码区。
    就怕你这个php文件的文件编码是utf-8编码,且有bom头,返回串中会多上3个字节(EF BB BF)。
      

  13.   

    刚自己找了下 发现问题在于 
    $conn=mysql_connect('localhost','root','root');
    mysql_select_db('shop', $conn);
    这两句我是用include("conn.php")这个引用的 只要引用就有问题 不引用就没事 是怎么一回事
      

  14.   

    conn.php文件都有什么东西?这个文件的文件编码是什么编码?
    还有你最好用firebug去看看ajax发送请求后返回串的具体内容,很方便的.
      

  15.   

    那就说明你的conn.php有错误了,仔细检查下,在页面中写跟用include效果应该是一样的。