Overloard Overloard - 1 year ago 97
PHP Question

Contact Form - Invalid email address entered! - Allow it to go through

I have a contact form on my simple HTML/PHP/CSS site, and I want to allow people to send their contact message even if they don't submit their email.

But everything I tried, when I submit a message through the contact form, it either returns some errors, or it returns the following error: "Invalid email address entered!"

Here is my contactprocess.php



if(!$_POST) exit;

$email = $_POST['Email_Address'];
$error ="";
$errors ="";

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error.="Invalid email address entered!";

if($errors==1) echo $error;
$values = array ('Link','Quantity','Your_Message');
$required = array('Link','Quantity','Your_Message');

$email_subject = "The Message";
$email_content = "new message:\n";

foreach($values as $key => $value){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
$email_content .= $value.': '.$_POST[$value]."\n";

if(@mail($your_email,$email_subject,$email_content)) {
header('Location: http://example.com');
} else {
echo 'ERROR!';

I'm sure it's a simple edit, but I just can't figure it out.

Answer Source

Your code explicitly prevents in going through & adds the error if the email is invalid?

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   $error.="Invalid email address entered!";


if($errors==1) echo $error;
else{ // other stuff

If you don't want that behavior, wouldn't the simplest option be to just remove that code?

Or is the idea, if they do submit an email it must be valid - else allow it through if there is none. In which case your if should be something like

if (!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download