if语句在ajax中不起作用

时间:2016-08-26 12:26:51

标签: php jquery ajax html5 if-statement

我按下登录按钮后尝试重定向用户。所以它发生了,并且if条件不满足,并且数据本身是正确的,没有额外的字符并且包含单个" A"。由于某种原因,它不会工作。 我试过使用trim()函数,但无济于事。 这是我的代码:

  $("#login").click(function(event){
        event.preventDefault();
        var email =  $("#email").val();
        var pass = $("#password").val();
        $.ajax({
            url: "login.php",
            method: "POST",
            data: {userLogin: 1, userEmail:email, userPassword:pass},
            success: function(data){
               if(data!= "A"){
                   alert(data);
                }else{
                    window.location.href="profile.php";
               }
            }

        })
    })

这是PHP:

<?php
include 'connection/connect.php';

session_start();

if(isset($_POST["userLogin"])){
    $email = mysqli_real_escape_string($con, $_POST["userEmail"]);
    $password = md5($_POST["userPassword"]);
    $sql = "SELECT * FROM user_info WHERE email = '$email' AND password = '$password'";
    $run_query = mysqli_query($con, $sql);
    $count = mysqli_num_rows($run_query);
    if($run_query===false){
            echo mysqli_error($con);
        }
        elseif ($count == 1){

        $row = mysqli_fetch_array($run_query);
        $_SESSION["uid"] = $row["user_id"];
        $_SESSION["name"] = $row["first_name"];
        echo trim("A");
    }
}
?>

以及HTML:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<li><a href="#" class="dropdown-toggle" data-toggle="dropdown">SignIn</a>
<ul class="dropdown-menu">
<div style="width: 300px;">
<div class="panel panel-primary">
<div class="panel-heading">Login</div>
<div class="panel-heading">
<label for="email">Email</label>
<input type="email" class="form-control" id="email"  name="email" required />
<label for="password">Password</label>
<input type="password" class="form-control" id="password"  name="password" required />
<p><br/></p>
<a href="#">Forgotten Password</a><input type="submit" class="btn btn-success" id="login" value="Login">
</div>
<div class="panel-footer" id="e_msg"></div>
</div>
</div>
</ul>
</li>
</body>
</html>

此外,当我直接进入页面&#34; profile.php&#34;时,用户已登录。这意味着数据正确传递。 可能是什么问题?谢谢。 编辑,警报打印&#34; A&#34;。

1 个答案:

答案 0 :(得分:0)

尝试javascript trim()方法来比较字符串。

if(data.trim()=="A"){
   window.location.href="profile.php";
}else{
   alert(data);
}