不要发送电子邮件PHP

时间:2016-01-29 19:37:36

标签: php

即使错误字段fnameErr, lnameErr为空,我的代码仍在打印"请勿发送电子邮件"

        // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
       if (empty($_POST["fname"])) {
         $fnameErr = "First Name is required";
       } else {
         $fname = test_input($_POST["fname"]);
         // check if name only contains letters and whitespace
         if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
           $fnameErr = "Only letters and white space allowed"; 
         }
         $stop=true;
       }
       if (empty($_POST["lname"])) {
         $lnameErr = "Last Name is required";
       } else {
         $lname = test_input($_POST["lname"]);
         // check if name only contains letters and whitespace
         if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
           $lnameErr = "Only letters and white space allowed"; 
         }
          $stop=true;
       }
       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"; 
         }
           $stop=true;
       }

       if (empty($_POST["phone"])) {
         $phoneErr = "Phone is required";
          $stop=true;
       }

       if (empty($_POST["comment"])) {
          $commentErr = "Comments is required";
           $stop=true;
       }


        if(!$stop)

        {
             echo "send an email";
        }

    }

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

我的HTML代码

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                                                <fieldset>
                                                  <h4>&nbsp;</h4>   
                                                    <div class="form-group"> 
                                                        <div class="col-md-6">
                                                            <input id="fname" name="fname" type="text" placeholder="First Name" class="form-control" value="<?php echo $_POST["fname"];?>"><span class="error"><?php echo $fnameErr;?></span>
                                                        </div>
                                                    </div>
                                                    <div class="form-group"> 
                                                        <div class="col-md-6">
                                                            <input id="lname" name="lname" type="text" placeholder="Last Name" class="form-control" value="<?php echo $_POST["lname"];?>"><span class="error"><?php echo $lnameErr;?></span>
                                                        </div>
                                                    </div>
                                                        <div class="form-group"> 
                                                        <div class="col-md-6">
                                                            <input id="email" name="email" type="text" placeholder="Email Address" class="form-control" value="<?php echo $_POST["email"];?>"><span class="error"><?php echo $emailErr;?></span>
                                                        </div>
                                                    </div>

                                                    <div class="form-group"> 
                                                        <div class="col-md-6">
                                                            <input id="phone" name="phone" type="text" placeholder="Phone" class="form-control" value="<?php echo $_POST["phone"];?>"><span class="error"><?php echo $phoneErr;?></span>
                                                        </div>
                                                    </div>

                                                    <div class="form-group"> 
                                                        <div class="col-md-6">
                                                            <textarea class="form-control" id="comment" name="comment" placeholder="Enter your message" rows="7"><?php echo $_POST["comment"];?></textarea><span class="error"><?php echo $commentErr;?></span>
                                                        </div>
                                                    </div>

                                                    <div class="form-group">
                                                        <div class="col-md-12 text-left">
                                                            <button type="submit"  name="submit" class="btn btn-primary readmmore">Submit</button>
                                                        </div>
                                                    </div>
                                                </fieldset>
                                            </form>

2 个答案:

答案 0 :(得分:2)

如果您只想让fNameErr和LnameErr停止发送,那么您将不得不使用:

if(empty($fnameErr) && empty(lnameErr) && empty(emailErr) && empty(phoneErr) && empty(commentErr))

    {

        echo "send an email";

    }

答案 1 :(得分:1)

试试这个(你在错误的地方设置$ stop为true):

        // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";

    // Let's explicityly set $stop to false(some PHP settings require this)
    $stop = False;

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

       }
       if (empty($_POST["lname"])) {
         $lnameErr = "Last Name is required";
         $stop=true;
       } else {
         $lname = test_input($_POST["lname"]);
         // check if name only contains letters and whitespace
         if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
           $lnameErr = "Only letters and white space allowed"; 
           $stop=true;
         }
       }
       if (empty($_POST["email"])) {
         $emailErr = "Email is required";
         $stop=true;
       } 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"; 
           $stop=true;
         }
       }

       if (empty($_POST["phone"])) {
         $phoneErr = "Phone is required";
         $stop=true;
       }

       if (empty($_POST["comment"])) {
          $commentErr = "Comments is required";
          $stop=true;
       }


   if ($stop){
      // Validation failed, dont send email
   } else {
      // Validation passed, send email
   }    


    }