在通过验证的PHP表单发送之前验证电子邮件

时间:2014-07-23 11:49:01

标签: php validation email

我正在使用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>

虽然服务器端验证工作正常,但只有当$ name(视为主题)和$ comment(视为消息)字段都被填充时,我才能使mail()函数起作用。 我尝试了很多组合,但即使字段为空,也始终提交电子邮件。

所以我的问题是:如果填写并验证$ name和$ comments字段,如何使这个经过验证的表单发送电子邮件?

3 个答案:

答案 0 :(得分:0)

您可以随时使用标记。

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

验证$ name和$ comment后进行验证:

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