I want to redirect the page after login succesfully completed. but the problem in this code is, if i give wrong input, it also go to home page. so i removed this statement 'window.location = home.php'. what could i do now? does anyone help me guys?
<script>
$(document).ready(function(){
$('#reg_form').parsley();
$('#reg_form').on('submit',function(event){
event.preventDefault();
if($('#reg_form').parsley().isValid())
{
$.ajax({
url:"db.php",
method:"POST",
data:$(this).serialize(),
beforeSend:function(){
$('#submit').attr('disabled','disabled');
$('#submit').val('submitting the value');
},
success:function(data){
$('#reg_form')[0].reset();
$('#reg_form').parsley().reset();
$('#submit').attr('disabled',false);
$('#submit').val('submit');
$('#message').html(data);
}
});
}
});
});
</script>
db.php
<?php
//user login
if(isset($_POST['email']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$query = "SELECT * FROM tbl_register where email ='$email' AND password = '$password'";
$result = mysqli_query($connect,$query);
if(mysqli_num_rows($result)>0)
{
echo "login successfully completed";
}
else
{
echo "login failed";
}
}
?>
Its better to pass flag value that login successfully or not from server side.
Like:
If you will write below code:
And then in ajax call you can check data
I hope my answer helps you.
In your php file
and in ajax success check for the status from data as
Please always use the sessions in such login scenarios. you can set the session value if the login information is correct. Then check the session value in home.php, redirect the page from home.php to login page if session value is not correct. use the following code.
Now in "home.php" check whether the user is logged in or not.