简体   繁体   English

停止发送电子邮件和成功消息

[英]Stop Sending email and Success Message

Hi I am a beginner and php. 嗨,我是初学者和PHP。 I have used this validation example from w3 schools...The form is validating and sending the mail correctly. 我使用了来自w3学校的此验证示例...该表单正在正确验证和发送邮件。 However. 然而。 If i press submit without filling anything. 如果我按提交而不填写任何内容。 It still sends an empty email. 它仍然发送空电子邮件。 How to prevent it from not sending email unless all the fields are filled...Also how to show a Thank you message once everything is validated. 除非所有字段都已填写,否则如何防止它不发送电子邮件...验证所有内容后如何显示“谢谢”消息。 I am very new to php. 我对php很陌生。 It would be great if you guys can copy my code and fiddle it. 如果你们可以复制我的代码并弄弄它,那就太好了。 So that i can copy paste. 这样我就可以复制粘贴了。

Mail to Function code: 邮件转功能码:

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname:</th>
<th>Email:</th>
<th>Interest:</th>
<th>comment:</th>
</tr>
<tr>
<td>$name</td>
<td>$email</td>
<td>$int</td>
<td>$comment</td>
</tr>
</table>
</body>
</html>
";
?>


<?php
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?>

validation code: 验证码:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $intErr = "";
$name = $email = $gender = $comment = $int = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameErr = "Only letters and white space allowed"; 
     }
   }

   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
     // check if e-mail address is well-formed
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Invalid email format"; 
     }
   }

   if (empty($_POST["int"])) {
     $intErr = "Interest is required";
   } else {
     $int = test_input($_POST["int"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$int)) {
       $intErr = "Only letters and white space allowed"; 
     }
   }

   if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }

   if (empty($_POST["gender"])) {
     $genderErr = "Gender is required";
   } else {
     $gender = test_input($_POST["gender"]);
   }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

First, I would suggest you to do the form validation on client side using javascript, and not on server side. 首先,我建议您在客户端使用javascript而不是在服务器端进行表单验证。 It would help server to bear less load. 这将帮助服务器减轻负载。 You can search on Google how to validate form using javascript before submitting. 提交之前,您可以在Google上搜索如何使用javascript验证表单。

If you still wish to do it on server side, then the moment you find an empty field, you can simply redirect the user to form page using "Header", with showing the empty field and asking them to fill it again. 如果您仍然希望在服务器端执行此操作,那么当您找到一个空白字段时,您可以使用“标题”将用户重定向到表单页面,只需显示该空白字段并要求他们再次填写即可。 (You can again see the importance of Javascript, it would save the user his/her time to wait for server response). (您可以再次看到Javascript的重要性,它将为用户节省等待服务器响应的时间)。

header("Location:Your-FormFile-Name");       

Only when everything is filled and not empty, you use mail function. 仅当所有内容都已填充且不为空时,才使用邮件功能。

:-) :-)

Hi buddy in your both page code is looking fine. 嗨,您的两个页面代码中的好友看起来都很好。 but have no relation. 但没有关系。

your html form is not submitted without form tag. 您的html表单没有表单标签就不会提交。 and validation code file is not call in html page. 验证代码文件未在html页面中调用。 When you refresh your page then mail is automatically goes. 刷新页面后,邮件自动发送。

please check this link this help you.. http://www.formget.com/jquery-login-form/ 请检查此链接,这对您有帮助。.http ://www.formget.com/jquery-login-form/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM