So I have been searching for questions like "randomly redirects back to index.php" and "form not submitting correctly" but none of the questions seem to address my problem so was thinking maybe my problem is a specific one. (may contain more than one error).
Basically I have a login page that is supposed to redirect to register.php if username and password does not match any of those in the database, and redirect to index.php if both username(email) and password matches.
login.php
<?php
include "header.php";
?>
<!--follow from header.php-->
<div class="ui centered grid" id="loginbox">
<div class="ui inverted segment">
<form class="ui inverted form" action="login.php" method="post" enctype="multipart/form-data">
<div class="two fields">
<div class="required field">
<label>Username</label>
<input placeholder="Username" type="text" name="log_email" required/>
</div>
<div class="required field">
<label>Password</label>
<input placeholder="Password" type="text" name="log_password" required/>
</div>
</div>
<button class="ui submit button" name="login" type="submit"><a href="index.php">Log In</a></button>
<button class="ui button"><a href="register.php">Register</a> </button>
</form>
</div>
</div>
<!--continue to footer_form.php-->
<?php
if(isset($_POST['login'])){
$log_email = $_POST['log_email'];
$log_password = $_POST['log_password'];
$sel_log = "select * from customers where email ='$log_email' and password ='$log_password'";
$run_log = mysqli($mysqli,$sel_log);
$check_customer = mysqli_num_rows($run_log);
if($check_customer==0){
echo"<script>alert('Please register first!')</script>";
header("Location: register.php");
exit;
}
if($check_customer>0){
echo"<script>alert('Logged in successfully!')</script>";
header("Location: index.php");
exit;
}
}
include "footer_form.php" ?>
TLDR; My problems are: 1) Alert script does not seem to be working for both cases of IF 2) Once in a while when clicking "Log In" on the login.php page, it stays on the same page, sometimes it goes to index.php, very inconsistent, regardless of your input in username and password.
Database side all naming and connections are correct.
Edit: The php form problem has been solved by the friends below in the comment section. I just want to share with you an answer that solved the script problem.
if($check_customer==0){
echo"<script>alert('Please register first!');window.location='register.php'</script>";
exit;
}
Using the code above solved the problem as it seems alert script and header is not compatible, using window.location solved it gracefully.