I am trying to make a login with ajax, I want the user to login and if the username(email) and password are correct the page should use the method .load to load the page "perfil.php", that I won't post the code since it isn't relevant. However nothing is happening even if i type random user/password.
I just started with SESSION's and AJAX so any help would be appreciated, BTW i don't need answers talking about hashing the password at this moment.
index
<?php
session_start();
if(!empty($_SESSION['login_user'])){
header('Location: user/perfil.php');
}
?>
<form class="form-signin" id="form-signin" method="post" action="">
<h2 class="form-signin-heading">Sign in</h2>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required="true" autofocus="true">
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required="true">
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Lembrar-me
</label>
</div>
<button id="entrar" class="btn btn-lg btn-primary btn-block" type="submit" name="submit">Sign in</button>
login.php
<?php
include_once '../config/db.php';
session_start();
if (isSet($_POST['email']) && isSet($_POST['password'])) {
try {
$pdo = Database::connect();
$email = $_POST['email'];
$password = $_POST['password'];
$query= "SELECT id FROM utilizador WHERE email = '$email' and pass = '$password'";
$stmt = $pdo->prepare($query);
$stmt->execute();
$num=$stmt->rowCount();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($num > 0)
$_SESSION['login_user']=$row['id'];
echo $row['id'];
}
Database::disconnect();
}
catch (Exception $e){
echo 'Exception: ', $e->getMessage(), "
";
}
}
?>
javascript
$('#entrar').click(function()
{
var email = $("#inputEmail").val();
var password = $("#inputPassword").val();
var dataString = 'email='+email+'&password='+password;
if($.trim(username).length>0 && $.trim(password).length>0)
{
$.ajax({
type: "POST",
url: "user/login.php",
data: dataString,
cache: false,
beforeSend: function(){ $("#entrar").val('A entrar...');},
success: function(data){
if(data)
{
showNotas();
$('#form-signin').fadeOut('slow', function(){
$("#section-1 .container").load("user/perfil.php").fadeIn(1500).delay(5000);
$("#section-1 span").text("Perfil");
});
$('#notas').show();
}
else
{
$("#entrar").val('Sign in')
$("#error").html("<span style='color:#cc0000'>Error:</span> Invalid username and password. ");
}
}
});
}
return false;
});
EDIT: Chrome dev isn't displaying any error