在从经过验证的PHP表单发送电子邮件之前进行验证


Validating email before sending it from validated PHP form

我使用来自W3 (http://www.w3schools.com/php/php_form_complete.asp)的代码为给定的示例表单进行服务器端验证。

<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
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 syntax is valid
     if (!preg_match("/(['w'-]+'@['w'-]+'.['w'-]+)/",$email)) {
       $emailErr = "Invalid email format";
     }
   }
   if (empty($_POST["website"])) {
     $website = "";
   } else {
     $website = test_input($_POST["website"]);
     // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
     if (!preg_match("/'b(?:(?:https?|ftp):'/'/|www'.)[-a-z0-9+&@#'/%?=~_|!:,.;]*[-a-z0-9+&@#'/%=~_|]/i",$website)) {
       $websiteErr = "Invalid URL"; 
     }
   }
   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;
}

?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
   Name: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   Website: <input type="text" name="website" value="<?php echo $website;?>">
   <span class="error"><?php echo $websiteErr;?></span>
   <br><br>
   Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <br><br>
   Gender:
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">Female
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">Male
   <span class="error">* <?php echo $genderErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>
<?php

echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;

?>
</body>
</html>

虽然服务器端验证工作得很好,但我不能让mail()函数只有在$name(作为主题)和$comment(作为消息)字段都被填充时才能工作。我试过很多组合,但是电子邮件总是提交,即使字段是空的。

所以我的问题是:如何使这个验证表单发送电子邮件只有当$name和$comment字段填充和验证?

当名称无效时创建$nameErr,当注释有效时创建$comment为空字符串,您可以使用以下条件:

if (!$nameErr && $comment) {
}

空字符串被认为是FALSE:http://php.net/manual/en/language.types.boolean.php

你总是可以使用标志。

<?php
  ....
   // let's say there are some flags for validation
   if($flag1 && $flag2 && .. $flagN){
     // perform email sending here
   }
?>

或者您可以使用默认的错误消息变量;检查它们是否为空

<?php
       if(!empty($nameErr) && !empty($comment)){
         // perform email sending
       }
?>

在验证了$name和$comment之后执行验证:

 if($nameErr == "" && $emailErr == "") { // If they are empty, no errors where found
 // Do your email validation here
 }