【问题标题】:Stop Sending email and Success Message停止发送电子邮件和成功消息
【发布时间】:2015-06-18 13:14:11
【问题描述】:

你好我是一个初学者和 php。我使用了 w3 学校的这个验证示例......表单正在验证并正确发送邮件。然而。如果我按提交而不填写任何内容。它仍然发送一封空电子邮件。除非所有字段都已填写,否则如何防止它不发送电子邮件...以及如何在所有内容都得到验证后显示感谢消息。我对php很陌生。如果你们能复制我的代码并摆弄它,那就太好了。这样我就可以复制粘贴了。

邮件到功能代码:

$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);
?>

验证码:

<?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;
}
?>

【问题讨论】:

    标签: php validation email


    【解决方案1】:

    首先,我建议您使用 javascript 在客户端进行表单验证,而不是在服务器端。这将有助于服务器承担更少的负载。您可以在 Google 上搜索如何在提交之前使用 javascript 验证表单。

    如果您仍希望在服务器端执行此操作,那么当您找到一个空字段时,您可以简单地将用户重定向到使用“标题”的表单页面,显示空字段并要求他们再次填写。 (您可以再次看到 Javascript 的重要性,它可以节省用户等待服务器响应的时间)。

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

    只有当一切都被填满而不是空的时候,你才使用邮件功能。

    【讨论】:

      【解决方案2】:

      :-)

      嗨,您的两个页面代码中的好友看起来都很好。但没有关系。

      您的 html 表单没有表单标签就不会提交。并且验证代码文件不在 html 页面中调用。当您刷新页面时,邮件会自动发送。

      请检查此链接这对您有帮助.. http://www.formget.com/jquery-login-form/

      【讨论】:

      • 我在尝试提交数据时收到此错误。我使用 Javascript 验证已弃用:函数 eregi() 在第 548 行的 /home/content/99/10169899/html/demo/include/fgcontactform.php 中已弃用 这是该行的代码。返回eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA- Z]{2,6}$", $email);
      • 只有在表单中使用
        标签时才会提交表单。但我看不到表单标签。
      猜你喜欢
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2015-05-15
      相关资源
      最近更新 更多