It sends the form but when there’s an error it does not filter or show validation errors.
This php is not on the same page as form…it is separate…
<form method="post" action="http://www.domain.com/submit/send2.php">
<?php
//let's start the session
session_start();
$errFirstName ="";
$errLastName ="";
$errCellPhone ="";
$errEmail ="";
if ($_POST["submit"]) {
$FirstName = filter_var($_POST['FirstName'], FILTER_SANITIZE_STRING);
$LastName = filter_var($_POST['LastName'], FILTER_SANITIZE_STRING);
$Email = filter_var($_POST['Email'], FILTER_SANITIZE_EMAIL);
$Amount = filter_var($_POST['Amount'], FILTER_SANITIZE_NUMBER_INT);
$CellPhone = $_POST['CellPhone'];
$to = 'yourdomain@gmail.com';
$cc_tomail = 'user@domain.com';
$from = 'From: User<admin@domain.com>';
$host = "mail.domain.com";
$username = "admin@domain.com";
$password = "hissyfit";
$subject = 'Main subject';
$headers = "From: $from \r\n";
$headers .= "Reply-To: $Email \r\n";
$body = "From: $FirstName $LastName\n E-Mail: $Email\n Cell Phone: $CellPhone\n Phone: $HomePhone\n Amount: $Amount\n;
$autoResponse = true; //if set to true auto response email will be sent, if you don't want autoresponse set it to false
$autoResponseSubject = "Your request from User **Not Spam **";
$autoResponseMessage = "Hello, thank you for sending your request. We will contact you within 24 to 48 hours.";
$autoResponseHeaders = "From: noreply@domain.com";
// Check if name has been entered
if ($_POST['FirstName'] != "") {
$_POST['FirstName'] = filter_var($_POST['FirstName'], FILTER_SANITIZE_STRING);
if ($_POST['FirstName'] == "") {
$errors .= 'Please enter a valid first name.<br/><br/>';
}
} else {
$errors .= 'Please enter your first name.<br/>';
}
// Check if last name has been entered
if ($_POST['LastName'] != "") {
$_POST['LastName'] = filter_var($_POST['LastName'], FILTER_SANITIZE_STRING);
if ($_POST['LastName'] == "") {
$errors .= 'Please enter a valid last name.<br/><br/>';
}
} else {
$errors .= 'Please enter your last name.<br/>';
}
// Check if last name has been entered
if ($_POST['Amount'] != "") {
$_POST['Amount'] = filter_var($_POST['Amount'], FILTER_SANITIZE_NUMBER_INT);
if ($_POST['Amount'] == "") {
$errors .= 'Please enter the amount.<br/><br/>';
}
} else {
$errors .= 'Please enter your amount.<br/>';
}
// Check if email has been entered and is valid
if (!$_POST['Email'] || !filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!empty($_POST['CellPhone'])) {
$CellPhone = $_POST['CellPhone'];
$pattern = "/^[0-9\_]{7,20}/";
if (preg_match($pattern,$CellPhone)){ $CellPhone = $_POST['CellPhone'];}
else{ $errors[] = 'Your Cell Phone number can only be numbers.';}
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errCellPhone) {
if (mail ($to, $subject, $body, $from)) {
header( 'Location: http://www.domain.com/thank-you' ) ;
// $result='<div class="alert alert-success">Thank You! You will be contacted within 24 hours, allow 48 hours on weekends</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again </div>';
}
}
}
ob_end_flush();
?>