Here’s my problem:
I have two forms on the same page.
The first form works fine.
But whenever I click the submit button on the second form, I get two notices.
The first says “undefined index: email” which is referring to “$email = $_POST[“email”];” in my coding.
The second says “undefined index: password” which is referring to “$password = $_POST[“password”];” in my coding.
I do not want my 2nd form to be affected by all the “if statements” I created for the first form. And I’m going to be creating more “if statements” for the 2nd form soon.
For example, when I hit the submit button on the second form, it’s echoing “You need to enter an email and password” which is an error I only wanted applied to the first form.
So MY QUESTION IS: How can I get the forms to only be affected by a specific group of “if statements”? Like in css coding you apply ids, but what would i do to the forms or “if statements” so the “if statements” only affect specific forms?
Here’s the coding:
<form action="login.php" method="post">
<ul id="login">
//login information
<li id="loginn">
<input type="submit" value="Log in">
</li>
</ul>
</form>
<form action="" method="post">
<ul id="register">
//register info list items
<li>
<input type="submit" value="Sign up">
</li>
</ul>
</form>
<?php
if (empty($_POST) === false) {
$email = $_POST["email"];
$password = $_POST["password"];
if (empty($email) === true || empty($password) === true) {
$errors[] = "You need to enter an email and password.";
} else if (user_exists($email) === false) {
$errors[] = "The email you entered is not in our records. Have you registered?";
} else if (user_active($email) === false) {
$errors[] = "Go to your email, open the email we sent you,and activate your account.";
} else {$login = login($email, $password);
if ($login === false) {
$errors[] = "That email/password combination is incorrect.";
} else {
$_SESSION['users_id'] = $login;
header('Location: homepage.php');
exit();
}
}
}
if (empty($errors) === false) {
?>
<?php
echo output_errors($errors);
}
?>
You could add a
<hidden>form field with the form’s name:Then, in the PHP, use that to discern the forms:
(Note there is no error checking whatsoever in this code!)