每页刷新/现场访问后提交表单(PHP)

时间:2018-01-09 12:20:16

标签: php html forms submit

我的网站中有一个HTML表单,在每次页面刷新和网站访问时,表单都会提交并发送一封空白电子邮件。更奇怪的是,我在表单中有表单验证和必填字段,当我单击提交按钮时,必要的字段检查和其他验证相应地运行但是,当我刷新页面时 - 表单空白,没有输入 - 它提交表单和我收到一封空白电子邮件 - 如果能够在页面刷新时提交表单而没有输入值?希望这很清楚......下面是我的PHP和HTML表单。谢谢!

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

            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["comment"])) {
                $comment = "";
              } else {
                $comment = test_input($_POST["comment"]);
              }


            }

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




            if(empty($nameErr) && empty($emailErr))
                {
                    $myemail = 'doreen.jones@flavoredesign.com';
                    $name = $_POST['name']; 
                    $email = $_POST['email']; 
                    $company_name = $_POST['company_name'];
                    $phone = $_POST['phone'];
                    $budget = $_POST['budget'];
                    $timeline = $_POST['timeline'];
                    $services = implode(', ', $_POST['services']);
                    $comment = $_POST['comment']; 
                    $source = $_POST['source'];

                    $to = $myemail; 
                    $email_subject = "A Message From: $name \n \n";

                    $email_body = "You have received a new message. ".
                    "Here are the details:\n Name: $name \n Email: $email \n Company Name: $company_name \n Telephone: $phone \n Services of Interest: $services \n Budget: $budget \n Timeline: $timeline \n Message:  $comment \n Source: $source"; 

                    $headers = "From: $myemail\n"; 
                    $headers .= "Reply-To: $email";

                    mail($to,$email_subject,$email_body,$headers);
                    //redirect to the 'thank you' page
                    header('Location: http://flavoredesign.com/index.php#contact_sec');
                } 


                if(empty($nameErr) && empty($emailErr) && !empty($name) && !empty($email)) {
                    echo "<p class='thank_you'>Thank you! Your message has been received. A Flavoredesign representative will be in touch with you shortly!</p>";

                }

            ?>

而Html是

<div id="contact_form">
  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>#contact_sec" method="post">
    <div class="container">
      <div class="row">
        <div class="col-lg-6 col-md-12" style="padding-left:0px !important;">
          <span class="error">
            <?php echo $nameErr;?>
          </span>
          <input type="text" name="name" placeholder=" Your Name*" value="<?php echo $name;?>">
          <span class="error">
            <?php echo $emailErr;?>
          </span>
          <input type="email" name="email" placeholder=" Your Email*" value="<?php echo $email;?>">
        </div>
        <div class="col-lg-6 col-md-12" style="padding-right:0px !important;">
          <input type="text" name="company_name" placeholder=" Company Name">
          <input type="tel" name="phone" placeholder=" Phone Number">
        </div>
      </div>   
      <br/>
      <div class="row no-gutters" style="margin-left: -15px; margin-right: -15px;">
        <div class="col-12"> 
          <label>What services are you interested in? (select all that apply)
          </label>
          <div id="checkboxes">
            <ul>
              <li>
                <input type="checkbox" id="c1" name="services[]" value="Website Design">
                <label for="c1">Website Design
                </label>
              </li>
              <li>
                <input type="checkbox" id="c3" name="services[]" value="UI/UX Design">
                <label for="c3">UI/UX Design
                </label>
              </li>
              <li>
                <input type="checkbox" id="c5" name="services[]" value="Print Design">
                <label for="c5">Print Design
                </label>
              </li>
              <li>
                <input type="checkbox" id="c6" name="services[]" value="Logo & Identity Design">
                <label for="c6">Logo & Identity Design
                </label>
              </li>
              <li>
                <input type="checkbox" id="c8" name="services[]" value="SEO & Marketing">
                <label for="c8">SEO & Marketing
                </label>
              </li>
            </ul>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-lg-6 col-md-12" style="padding-left:0px !important;">
          <select name="budget">
            <option selected="selected"> What's your budget?
            </option>
            <option value="-500"> less than $1,000
            </option>
            <option value="1000to5000"> $1,000 to $5,000
            </option>
            <option value="5000to10000"> $5,000 to $10,000
            </option>
            <option value="+10000"> more than $10,000
            </option>
          </select>
        </div>
        <div class="col-lg-6 col-md-12" style="padding-right:0px !important;">
          <input type="text" name="timeline" placeholder=" How soon do you need your project completed?">
        </div>
      </div>   
      <div class="row">
        <div class="col-12" style="padding-right:0px !important; padding-left:0px !important;">  
          <textarea name="comment" placeholder="Questions? Details? What else would you like me to know?"
                    <?php echo $comment;?>>
          </textarea>
        <select name="source">
          <option selected="selected"> Where did you hear about Flavoredesign?
          </option>
          <option value="online search"> online search
          </option>
          <option value="from a friend"> from a friend
          </option>
          <option value="social media"> social media
          </option>
          <option value="media advertisment"> media advertisement
          </option>
          <option value="other"> other
          </option>
        </select>
      </div>
    </div>
    </div>
  <div id="submit">
    <button type="submit" id="contact_button" class="buttons hvr-ripple-out">Submit
    </button>
  </div>
  </form>
</div>

0 个答案:

没有答案