I'm a little puzzled with how to implement the Post/Redirect/Get pattern in PHP. I've seen a few answers saying just add the header function after you've submitted the form; I've done that but upon validating whether the input fields are empty, it doesn't print anything although I've added the header function after it's ran the code. I simply cannot find how to integrate this, so I'm asking anyways.
<?php
require '../BACKEND/DB/DB_CONN.php';
if(isset($_POST['submit-register'])) {
if(empty($_POST['email'])) {
echo 'Email cannot be nothing.';
}
header("Location: index.php");
die();
}
if(isset($_POST['submit-login'])) {
if(empty($_POST['loginUser'])) {
echo 'Field Empty.';
}
header("Location: index.php");
die();
}
?>
<div class="forms">
<form method="post" action="index.php" role="form" class="forms-inline register ">
<h4>Register</h4>
<input type="text" name="email" placeholder="Email Address" autocomplete="off" />
<input type="text" name="username" placeholder="Username" autocomplete="off" />
<input type="password" name="password" placeholder="Password" autocomplete="off" />
<input type="submit" name="submit-register" value="Register" role="button" name="login-btn" />
</form>
<form method="post" action="index.php" role="form" class="forms-inline login ">
<h4>Login</h4>
<input type="text" name="loginUser" placeholder="Username" />
<input type="password" name="loginPass" placeholder="Password" />
<input type="submit" name="submit-login" value="Register" role="button" />
</form>
</div>
I was writing the code, but as I've found out it doesn't work, I'm asking how to fix this and how to implement the Post/Redirect/Pattern securely and I know it works.