pleaseForgiveCodeNoob pleaseForgiveCodeNoob - 5 months ago 18
HTML Question

php form not working no matter input, php script alert not working

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.


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">
<input placeholder="Username" type="text" name="log_email" required/>
<div class="required field">
<input placeholder="Password" type="text" name="log_password" required/>
<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>

<!--continue to footer_form.php-->

$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);
echo"<script>alert('Please register first!')</script>";
header("Location: register.php");
echo"<script>alert('Logged in successfully!')</script>";
header("Location: index.php");

include "footer_form.php" ?>

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.

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.

echo"<script>alert('Please register first!');window.location='register.php'</script>";

Using the code above solved the problem as it seems alert script and header is not compatible, using window.location solved it gracefully.


Your submit button goes to index.php:

<button class="ui submit button" name="login" type="submit">
  <a href="index.php">Log In</a>

Remove the anchor:

<button class="ui submit button" name="login" type="submit">Log In</button>