vendredi 26 août 2016

if statement not working inside ajax

I'm trying to redirect a user after he presses a sign-in button. So it happens, and the if condition is not met, and the data itself is correct with no extra characters and contains a single one "A". for some reason it won't work. I have tried to use the trim() function but to no avail. Here is my code:

  $("#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";
               }
            }

        })
    })

And this is the 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");
    }
}
?>

And the 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>

furthermore, when i go directly to the page "profile.php", the user is logged in. That means that the data is passed correctly. What can be the problem? Thanks.

Aucun commentaire:

Enregistrer un commentaire