隐藏的div在它不应该消失时消失

时间:2013-08-01 21:54:25

标签: javascript html

我有一个登录脚本的功能。如果结果不等于“成功”,则应该使用状态ID淡入div。无论如何,div开始消失。虽然,它登录并重定向,但您仍然可以看到div开始淡入。

JS:* FYI,_是document.getElementById *

的变量
function login() {
    var u = _("user").value;
    var p = _("pass").value;
    var status = _("status");

        var ajax = ajaxObj("POST", "login_script.php");
        ajax.onreadystatechange = function(){
            if(ajax.responseText !== "success"){
                status.innerHTML = ajax.responseText;
                if(_('status').style.display != "block"){
                        fadeIn('status');
                    };
            }
            else{
                window.location.href = "index.php";
            }
        };
        ajax.send(
            "u=" + u
            + "&p=" + p);
}

HTML:

  <form onsubmit="return false;" id="valid" class="mainForm">
            <fieldset>
                <div class="loginRow">
                    <label for="req1">Username:</label>
                    <div class="loginInput"><input type="text" name="user" id="user" /></div>
                </div>

                <div class="loginRow">
                    <label for="req2">Password:</label>
                    <div class="loginInput"><input type="password" name="pass" id="pass" /></div>
                </div>
                <div id="status" style="width:94%;"></div>
                <div class="loginRow">
                    <div class="submitForm">
                        <button id="loginbtn" onclick="login();" class="greyishBtn"> Login </button>
                    </div>
                    <div id="forgot" style="padding:5px 15px;; float:right;">
                        <a href="forgot.php">Forgot/Reset Password</a>
                    </div>
                </div>
            </fieldset>
        </form>

与登录的script.php

$username = $_POST['u'];
$password = $_POST['p'];

$query = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT username, password, id_user, permissions FROM users WHERE username='$username' and password='$password'");

$count = mysqli_num_rows($query);
$row = mysqli_fetch_assoc($query);
$perm = $row['permissions'];



if($count==1){
    $seconds = 3600 + time();
    setcookie(loggedin, date("F js - g:i a"), $seconds);
    session_start();
    $_SESSION['id'] = $row['id_user'];
    echo "success";
    }
    else{
        echo '<span style="color:red;">Please check your username and password and try again.</span>';
}

ajaxObj:

function ajaxObj(meth, url) {
    var x = new XMLHttpRequest();
    x.open(meth, url, true);
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}
function ajaxReturn(x){
    if(x.readyState == 4 && x.status == 200){
        return true;
    }
}

如果有人想测试一下:http://lucienconsulting.com/brick-academy/

用户名:jvincilione3@aol.com 密码:密码

你可以看到我在说什么。

2 个答案:

答案 0 :(得分:0)

你可以尝试为第二个如果添加另一个=符号:

if(_('status').style.display !== "block"){
                    fadeIn('status');
                };

如果这不起作用,你可以试试这个:

if(status.style.display !== "block"){
                        fadeIn('status');
                    };

答案 1 :(得分:0)

onreadystatechange可能不止一次。你要检查它是否完整。

ajax.onreadystatechange = function(){
  if (ajax.readyState == 4) {   // complete
        if(ajax.responseText !== "success"){
            status.innerHTML = ajax.responseText;
            if(_('status').style.display != "block"){
                    fadeIn('status');
                };
        }
        else{
            window.location.href = "index.php";
        }
  }
};