Every part of it works, except one thing.
When I test the form by entering an invalid email address, i get the echo "you have to enter a valid email."
But when I enter a valid email, it still gives me the same error echo.
What am i missing?
Code: Select all
<?php
$to = "myemailaddress@domain.com";
$subject = "Quick Contact Form Submission";
$firstname_field = $_POST['firstname'];
$lastname_field = $_POST['lastname'];
$email_field = $_POST['email'];
$message_field = $_POST['message'];
$body = "First: $firstname_field\n Last: $lastname_field\n E-Mail: $email_field\n Message: $message_field\n";
if(!isset($_POST['submit'])) {
header( "Location: http://www.bluetorchmedia.com/contact.php" );
}elseif (empty($firstname_field) || empty($lastname_field)) {
echo "you have to fill out everything";
}elseif (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email_field)) {
echo "you have to enter a valid email address";
}else{
mail($to, $subject, $body);
?>
<html>
<head>
</head>
<body>
Going to show stuff here.
<?
}
?>
</body>
</html>Then it makes sure certain fields were filled out.
Then it checks if the email is valid, if not then echo the error, if it's good send the mail() function and display the html.
If the email is valid it still shows the error echo only.
Please help.