THis My PHP code
<?php
//Include libraries
require __DIR__ . '/vendor/autoload.php';
//Create instance of MongoDB client
$mongoClient = (new MongoDBClient);
//Select a database
$db = $mongoClient->EcommerceWeb;
//Select a collection
$collection = $db->Employees_Data;
//Extract the data that was sent to the server
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
$findemail = [
"email" => $email,
"password" => $password,
];
$cursor = $collection->findOne($findemail);
if($cursor){
if($cursor['email'] == $email and $cursor['password'] == $password){
echo("<script>alert('User Successfully Added')</script>");
echo("<script>window.location = 'cms-view-products.html'</script>");
}
}
else {
echo("<script> alert('failed login') </script>");
echo("<script> window.location = 'login.html'</script>");
}
?>
AND this is my HTML code
<?php include('login.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>LogIn Demo</title>
</head>
<body>
<form action="login.php" method="post" >
Email: <input type="email" name="email" required >
Password: <input type="password" name="password" required >
<input type="submit">
<p id="feedback"></p>
</form>
</body>
</html>
What I want to do and what I have tried
I have tried to echo the massage by echoing JS code to change the innerHTML element
but didn’t work.
I tried to echo the massage using PHP which works for me but I can’t use echo then header to redirect to a different page in PHP, and I found something the $Session to solve the problem.
My question
Is there a way to use JS better than alert to produce a massage then redirect to another page in successful login OR stay on the same page if the login failed?