Why isn't my php echo not working using jquery? If the incorrect password or credentials part is executed the login2.php file loads with the message
I want to print the Invalid Credentials or Password message in the <p> tag but it takes me to another tab that is loading the login2.php file and printing the message there.
index.html
<form id="login-form" action="login2.php" method="POST">
<input type="text" name="email" id="login-email" class="form-control" placeholder="E-mail">
<br>
<input type="password" name="password" id="login-password" class="form-control" placeholder="Password">
<br>
<button type="submit" name="login" id="login-btn" class="btn btn-warning">Log In</button>
<br>
<p class="login-msg">
</p>
<br>
<a href="signup.php" class="text-secondary">Don't have an account? Sign up</a>
</form>
login2.php
<?php
include "connection.php";
if(isset($_POST["login"]))
{
$email = $_POST["email"];
$password = $_POST["password"];
$query="select * from user where email='$email'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);
$num_of_rows=mysqli_num_rows($result);
$user_id=$row["user_id"];
if($num_of_rows==1)
{
if($password == $row["password"])
{
$user_type=$row["user_type"];
if($user_type=="buyer")
{
$_SESSION["user_id"]=$user_id;
header("location:index.php");
}
else if($user_type=="seller")
{
$_SESSION["user_id"]=$user_id;
header("location:seller_homepage.php");
}
}
else{
echo "<span>Incorrect Password</span>";
}
}
else{
echo "<span>Invalid Credentials</span>";
}
}
?>
main.js
$('body').on('click','#login-btn',(e)=>{
e.preventDefault();
var email = $("#login-email").val();
var password = $("#login-email").val();
$.post("login2.php",{
email : email,
password : password
},(data)=>{
$(".login-msg").html(data);
})
})