带有php代码的表单在刷新页面时提交


The form with php code gets submitted on refreshing the page

请有人帮我找出为什么在刷新页面时提交此表单?验证工作良好,提交时也会提交详细信息。唯一的问题是此表单是在刷新联系人页面时提交的。

<!doctype html>
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" class="csstransforms csstransforms3d csstransitions"><head profile="http://gmpg.org/xfn/11">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Our Site</title>
<link href="media-queries.css" rel="stylesheet" type="text/css">
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="fonts.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="pagewrap"> <!--pagewrap start-->
<!--header start-->
<div id="header">
  <div class="bottom">
  </div><!--bottom end-->
</div> <!--header end-->
<!--content start-->
<div id="content_1">
  <div class="content_1_1">
    <span class="text9">Contact Us</span><br><hr style="width:670px; margin-left:90px">
    <div style="margin-left:90px; margin-right:0px">
      <?php
      $firstname = $lastname = $email = $telephone = $comments = "";
      $error = "";
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
           if (empty($_POST["firstname"])) {
                $error  = "Field required";
           } else {
                $firstname = check($_POST["firstname"]);
                // check if name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
                     $error  = "Field required"; 
                }
           }
           if (empty($_POST["lastname"])) {
                $error  = "Field required";
           } else {
                $lastname = check($_POST["lastname"]);
                // check if name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
                     $error  = "Field required"; 
                }
           }

           if (empty($_POST["email"])) {
                $error = "Field required";
           } else {
                $email = check($_POST["email"]);
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                     $error  = "Field required"; 
                }
           }
           if (empty($_POST["telephone"])) {
                $error  = "Field required";
           } else {
                $telephone = check($_POST["telephone"]);
                if (!preg_match("/^[0-9'_]{7,20}/",$telephone)) {
                     $error  = "Field required"; }
           }

           if (empty($_POST["comments"])) {
                $errors  = "Field required";
           } else {
                $comments = check($_POST["comments"]);
                if (!preg_match("/^[a-zA-Z ]*$/",$comments)) {
                     $error  = "Field required"; 
                }
           }
      }

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


      if (empty($error)) { 
           $from = "From: Our Site!"; //Site name
           // Change this to your email address you want to form sent to
           $to = "info@oursite.com"; 
           $subject = "Website Form " . $name . "";
           $message = "Message from " . $firstname . " " . $lastname . " 
           Email: " . $email ."
           Phone: " . $telephone . " 
           Comments: " . $comments ."";
           mail($to,$subject,$message,$from);
      }
      ?>
      <form name="contactform" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"  >
        <table width="450px">
          <tr>
            <td valign="top">
              <label for="firstname">First Name *</label>
            </td>
            <td valign="top">
              <input  type="text" name="firstname" maxlength="50" size="30">
              <span class="error" style="color:#C00;"><?php echo $error ;?></span>
            </td>
          </tr>
          <tr>
            <td valign="top">
              <label for="lastname"> Last Name *</label>
            </td>
            <td valign="top">
              <input  type="text" name="lastname" maxlength="50" size="30">
              <span class="error" style="color:#C00;"><?php echo $error ;?></span>
            </td>
          </tr>
          <tr>
            <td valign="top">
              <label for="email">Email Address *</label>
            </td>
            <td valign="top">
              <input  type="email" name="email" maxlength="80" size="30">
              <span class="error" style="color:#C00;"><?php echo $error ;?></span>
            </td>
          </tr>
          <tr>
            <td valign="top">
              <label for="telephone">Telephone Number</label>
            </td>
            <td valign="top">
              <input  type="number"  name="telephone" maxlength="30" size="30">
              <span class="error" style="color:#C00;"><?php echo $error ;?></span>
            </td>
          </tr>
          <tr>
            <td valign="top">
              <label for="comments">Comments *</label>
            </td>
            <td valign="top">
              <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
              <span class="error" style="color:#C00;"><?php echo $error ;?></span>
            </td>
          </tr>
          <tr>
            <td colspan="2" style="text-align:center">
              <input type="submit" value="submit" name="submit">
            </td>
          </tr>
        </table>
      </form>
      <br>
    </div>
    </div>  <!--content_1_1 end-->
  </div><!--content end-->
  <!--footer start-->
  <div id="footer">
    </div><!--footer end-->
    </div><!--pagewrap end-->
  </body>
</html>

将整个PHP封装在下面的条件语句中,因为您已经有了一个命名的提交按钮。

<?php
if(isset($_POST['submit'])){
$firstname = $lastname = $email = $telephone = $comments = "";
...
  mail($to,$subject,$message,$from);
  }
} // brace for if(isset($_POST['submit']))
?>

然而,您可以将HTML表单和PHP拆分为两个单独的文件,同时使用相同的条件语句,然后重定向到另一个页面。

要了解如何重定向,请参阅以下问答;堆栈上的A:

  • 如何在PHP中进行重定向

您使用:

if (empty($error)) {
mail();
}

在刷新时,$error可能是空的,因此表单是邮寄的。