I've made a PHP form to submit to self with error validation, but the form is not submitting. The idea is, when the user clicks on the submit button and hasn't filled in all required fields or email address they entered is flawed, then errors occur by adding an error class that's sorted by CSS. The CSS is fine, but the form is not submitting. I'd appreciate the help.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Email</title>
</head>
<body>
<?php
$error = '';
$to = "[email protected]";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"]) || empty($_POST["email"]) || empty($_POST["message"])) {
$error = 'class="error" ';
} else {
$name = stripslashes(trim($_POST["name"]));
$email = stripslashes(trim($_POST["email"]));
$message = stripslashes(trim($_POST["message"]));
$pattern = '/[\r\n]|Content-Type:|Bcc:|Cc:/i';
if (preg_match($pattern, $name) || preg_match($pattern, $email)) {
$error = 'class="error" ';
}
$emailIsValid = filter_var($email, FILTER_VALIDATE_EMAIL);
if ($name && $email && $emailIsValid && $message) {
$subject = "From $name";
$body = "Name: $name <br /> Email: $email <br /> Message: $message";
$headers = "Reply-To: $email";
$success = mail($to, $subject, $body, $headers);
if ($success) {
header("Location: /email/sent/");
} else {
header("Location: /error/");
}
}
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input <?php echo $error; ?>type="text" name="name" placeholder="Full Name" spellcheck="false">
<input <?php echo $error; ?>type="text" email="email" placeholder="Email Address" spellcheck="false">
<textarea <?php echo $error; ?>type="text" message="message" placeholder="Message" rows="6" spellcheck="false"></textarea>
<button type="submit" name="submitted">submit</button>
</form>
</body>
</html>