0

我有一个登录脚本的功能。如果结果不等于“成功”,它应该淡入具有状态 ID 的 div。无论如何,div都会开始淡入。尽管它会登录并重定向,但您仍然会看到 div 开始淡入。

JS:*仅供参考,_ 是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>';
}

ajax对象:

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 密码:密码

你可以看到我在说什么。

4

2 回答 2

0

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

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

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

if(status.style.display !== "block"){
                        fadeIn('status');
                    };
于 2013-08-01T22:03:31.217 回答
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";
        }
  }
};
于 2013-08-01T22:03:49.817 回答