我正在尝试添加到我当前的PHP脚本(下面),现在验证用户是否输入了正确的电子邮件。我的下面的PHP是将表单发送到电子邮件地址,现在我添加到其中以验证电子邮件。我究竟做错了什么?我得到$ output_form = true / false;来自Head First PHP& MySQL的。这本书没有教我如何添加到我原来的PHP脚本来发送电子邮件。
<?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\n" .
"Service: $what_service\n" .
"Telephone #: $mytelephone\n" .
"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; } }
?>
答案 0 :(得分:3)
您的上述代码有效,但问题似乎与您放置验证逻辑的位置有关。 试试这个:
<?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\n" .
"Service: $what_service\n" .
"Telephone #: $mytelephone\n" .
"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>';
}
} ?&GT;
答案 1 :(得分:0)
我刚刚重新安排了您的代码以首先验证电子邮件并通过有效的电子邮件发送邮件:
<?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\n" .
"Service: $what_service\n" .
"Telephone #: $mytelephone\n" .
"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';
}
}
}
?>
答案 2 :(得分:0)
请尝试以下代码,
<?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\n" .
"Service: $what_service\n" .
"Telephone #: $mytelephone\n" .
"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';
}
}
}
?>