哪位大侠帮帮我,看看这段代码,为什么无法显跳出登入成功页面而且无法转到指定的主页面,但实际上登入成功了,<form id="form" name="form" method="post"  action="">
  <div id="container" align="center">
    <div id="bgdiv">
      <div id="txtdiv1">用户名:
        <input id="lgname" name="lgname" type="text" class="txt"/>
      </div>
      <div id="txtdiv2">密码:&nbsp;
        <input id="lgpwd" name="lgpwd" type="password" class="txt"/>
      </div>
      <div id="txtdiv3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="lgchk" type="lgchk" id="lgchk" style="width:40px;" maxlength="4">
      <img src="identifying.php" width="57" height="22" id='chk_id'/>
      <div id="btndiv">
        <input type="submit" name="lgbtn" value="登录" id="lgbtn" >
        &nbsp;
        <input type="button" name="fdbtn" value="重置" id="fdbtn" onClick="form.reset();return false;">
        &nbsp;
        <input id="chknm" name="chknm" type="hidden" value=""  />
      </div>
      <div id="regimg" style=" visibility: hidden;">&nbsp;</div>
    </div>
  </div>
</form>// JavaScript Document
function $(id){
return document.getElementById(id);
}
window.onload = function(){ $('lgbtn').onclick = check_login;

function check_login(){
if($('lgname').value.match(/^[a-zA-Z_]\w*$/) == null){alert('请正确输入用户名');return false;}
if($('lgpwd').value==''){alert('请输入密码');return false;}
if($('lgchk').value==''){alert('请输入验证码');return false;}


var xmlHttp;
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
} else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
var name=document.form.lgname.value;
xmlHttp.open("GET","chk_login.php?name="+name,true);
xmlHttp.onreadystatechange = function(){ alert('dfg');     if(xmlHttp.readyState == 4 ){
if(xmlHttp.status == 200) {
         result = xmlHttp.responseText;
 if (result=='1'){alert('登入成功');location = 'huixinhouse.php';}
 else if(result=='0'){alert('用户名或密码输入错误');locatioin = 'login.php';}
 else if(result=='-1'){alert('验证码输入错误');location = 'login.php';}
}
}
}
}
}<?php
session_start();
error_reporting(0);
include("conn.php");
$name = $_POST['lgname'];
$pwd = md5($_POST['lgpwd']);
$createtime = date("Y-m-d H:i:s");
$identifying=$_SESSION['identifying'];
$identifying_post=$_POST['lgchk'];
if ($identifying==$identifying_post){ $sql = mysql_query("select * from tb_user where username = '$name' and password = '$pwd'");
$ob1 = mysql_fetch_object($sql); if ($name == $ob1->username && $pwd == $ob1->password){
$reback='1';
$_SESSION["name"]=$name;
}else{
$reback='0';
}
}else{$reback= '-1';}echo $reback;
?>

解决方案 »

  1.   


    JS代码修改为如下即可:
    // JavaScript Document
    function $(id){
        return document.getElementById(id);
    }
    window.onload = function(){    $('lgbtn').onclick = check_login;
        
        function check_login(){
        if($('lgname').value.match(/^[a-zA-Z_]\w*$/) == null){alert('请正确输入用户名');return false;}
        if($('lgpwd').value==''){alert('请输入密码');return false;}
        if($('lgchk').value==''){alert('请输入验证码');return false;}
            
        
        var xmlHttp;
        if(window.ActiveXObject) {
            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        } else if(window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
        var name=document.form.lgname.value;
        xmlHttp.open("GET","chk_login.php?name="+name,true);
        xmlHttp.onreadystatechange = function(){    alert('dfg');        if(xmlHttp.readyState == 4 ){
            if(xmlHttp.status == 200) {
             result = xmlHttp.responseText;
             if (result=='1'){alert('登入成功');location.href = 'huixinhouse.php';}
             else if(result=='0'){alert('用户名或密码输入错误');locatioin.href = 'login.php';}
             else if(result=='-1'){alert('验证码输入错误');location.href = 'login.php';}
            }
        }
    }
    }
    }
      

  2.   


    汗,编辑器有问题,重新发一下:
    // JavaScript Document
    function $(id){
        return document.getElementById(id);
    }
    window.onload = function(){    $('lgbtn').onclick = check_login;
        
        function check_login(){
        if($('lgname').value.match(/^[a-zA-Z_]\w*$/) == null){alert('请正确输入用户名');return false;}
        if($('lgpwd').value==''){alert('请输入密码');return false;}
        if($('lgchk').value==''){alert('请输入验证码');return false;}
            
        
        var xmlHttp;
        if(window.ActiveXObject) {
            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        } else if(window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
        var name=document.form.lgname.value;
        xmlHttp.open("GET","chk_login.php?name="+name,true);
        xmlHttp.onreadystatechange = function(){    alert('dfg');        if(xmlHttp.readyState == 4 ){
            if(xmlHttp.status == 200) {
             result = xmlHttp.responseText;
             if (result=='1'){alert('登入成功');location.href = 'huixinhouse.php';}
             else if(result=='0'){alert('用户名或密码输入错误');locatioin.href = 'login.php';}
             else if(result=='-1'){alert('验证码输入错误');location.href = 'login.php';}
            }
        }
    }
    }
    }
    PS: 不能正确跳转的原因在于location.href='要跳转的URL',而楼主的代码中少了.href,因此不能正确跳转