I am trying to add onto my current PHP script (below) to now validate if a user put in a correct Email. My PHP below is to send a form to an Email address, and now I added onto it to validate the Email. What am I doing wrong? I am getting the $output_form = true/false; from the book Head First PHP & MySQL. The book does not teach me how to add onto my original PHP script to send an Email.
<?php
$myname = $_POST['name'];
$myemail = $_POST['email'];
$mytelephone = $_POST['telephone'];
$what_service = $_POST['service'];
$mycomments = $_POST['comments'];
if (isset($POST['submit'])) {
$to = 'example@aol.com';
$subject = 'Contact Us - My Business';
$msg = "Name: $myname
" .
"Service: $what_service
" .
"Telephone #: $mytelephone
" .
"Comments: $mycomments";
mail ($to, $subject, $msg, 'From:' . $myemail);
$output_form = false;
echo '<p>Thank you for contacting us!</p>';
echo 'Your Name: ' . $myname . '<br>';
echo 'Your E-Mail: ' . $myemail . '<br>';
echo 'Your Telephone: ' . $mytelephone . '<br>';
echo 'Your Service: ' . $what_service . '<br>';
echo 'Your Comments: ' . $mycomments;
if (!filter_var($myemail, FILTER_VALIDATE_EMAIL)) {
echo ' Invalid Email, please resubmit form.<br>';
$output_form = true; } }
?>
I just rearranged your code to validate email first and mail on valid email:
<?php
$myname = $_POST['name'];
$myemail = $_POST['email'];
$mytelephone = $_POST['telephone'];
$what_service = $_POST['service'];
$mycomments = $_POST['comments'];
if (isset($POST['submit'])) {
$to = 'example@aol.com';
$subject = 'Contact Us - My Business';
$msg = "Name: $myname
" .
"Service: $what_service
" .
"Telephone #: $mytelephone
" .
"Comments: $mycomments";
if (!filter_var($myemail, FILTER_VALIDATE_EMAIL)) {
echo ' Invalid Email, please resubmit form.<br>';
$output_form = true;
} else {
if (mail ($to, $subject, $msg, 'From:' . $myemail)) {
echo '<p>Thank you for contacting us!</p>';
echo 'Your Name: ' . $myname . '<br>';
echo 'Your E-Mail: ' . $myemail . '<br>';
echo 'Your Telephone: ' . $mytelephone . '<br>';
echo 'Your Service: ' . $what_service . '<br>';
echo 'Your Comments: ' . $mycomments;
} else {
eccho 'Failed';
}
}
}
?>
Your above code works, but the issue seems to be with where you place your validation logic. Try this:
<?php
error_reporting(E_ALL);
$myname = $_POST['name'];
$myemail = $_POST['email'];
$mytelephone = $_POST['telephone'];
$what_service = $_POST['service'];
$mycomments = $_POST['comments'];
if (isset($_POST['submit'])) { #this was $POST . Maybe a typo
if (!filter_var($myemail, FILTER_VALIDATE_EMAIL)) {
#the email is invalid. We therefore need to output the form
$output_form = true;
} else {
#the email is valid. We do not need to output the form
$output_form = false;
}
if ($output_form == false) {
#Since we do not need to output the form, lets proceed with sending the mail
$to = 'example@aol.com';
$subject = 'Contact Us - My Business';
$msg = "Name: $myname
" .
"Service: $what_service
" .
"Telephone #: $mytelephone
" .
"Comments: $mycomments";
if (mail ($to, $subject, $msg, 'From:' . $myemail)) { #returns true/false
echo '<p>Thank you for contacting us!</p>';
echo 'Your Name: ' . $myname . '<br>';
echo 'Your E-Mail: ' . $myemail . '<br>';
echo 'Your Telephone: ' . $mytelephone . '<br>';
echo 'Your Service: ' . $what_service . '<br>';
echo 'Your Comments: ' . $mycomments;
exit;
} else {
#lets try and get the reason for this happening.
print_r(error_get_last()); # this will return array of code, error and message
exit;
}
} else {
echo ' Invalid Email, please resubmit form.<br>';
echo '<form><h1>TRY AGAIN</h1></form>';
}
} ?>
Please try the below code,
<?php
if (isset($POST['submit'])) {
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
echo ' Invalid Email, please resubmit form.<br>';
// $output_form = true;
} else {
$myname = $_POST['name'];
$myemail = $_POST['email'];
$mytelephone = $_POST['telephone'];
$what_service = $_POST['service'];
$mycomments = $_POST['comments'];
$to = 'example@aol.com';
$subject = 'Contact Us - My Business';
$msg = "Name: $myname
" .
"Service: $what_service
" .
"Telephone #: $mytelephone
" .
"Comments: $mycomments";
if(mail($to, $subject, $msg, 'From:' . $myemail)) {
//$output_form = false;
echo '<p>Thank you for contacting us!</p>';
echo 'Your Name: ' . $myname . '<br>';
echo 'Your E-Mail: ' . $myemail . '<br>';
echo 'Your Telephone: ' . $mytelephone . '<br>';
echo 'Your Service: ' . $what_service . '<br>';
echo 'Your Comments: ' . $mycomments;
} else {
echo 'Error in sending email';
}
}
}
?>