如何以联系表格设置验证

时间:2014-06-10 15:03:32

标签: php validation contact contact-form

如何设置联系人验证 假设用户提交空字段然后如何显示无效输入 现在电子邮件地址只有无效输入显示我想修复所有字段验证请帮助我如何做到这一点 提前谢谢

 <html>
    <body>
    <?php
    function spamcheck($field) {
      // Sanitize e-mail address
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
      // Validate e-mail address
      if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
        return TRUE;
      } else {
        return FALSE;
      }
    }
    ?>


   <h2>Form</h2>
    <?php
    // display form if user has not clicked submit
    if (!isset($_POST["submit"])) {
      ?>
      <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
      From: <input type="text" name="email"><br>
      Subject: <input type="text" name="subject"><br>
      Message: <textarea rows="10" cols="40" name="message"></textarea><br>
      <input type="submit" name="submit" value="Submit Feedback">
      </form>
      <?php 
    } else {  // the user has submitted the form
      // Check if the "from" input field is filled out
      if (isset($_POST["email"])) {
        // Check if "from" email address is valid
        $mailcheck = spamcheck($_POST["email"]);
        if ($mailcheck==FALSE) {
          echo "Invalid input";
        } else {
          $email = $_POST["email"]; // sender
          $subject = $_POST["subject"];
          $message = $_POST["message"];
          // message lines should not exceed 70 characters (PHP rule), so wrap it
          $message = wordwrap($message, 70);
          // send mail
          mail("demo@gmail.com",$subject,$message,"From: $email\n");
          echo "Thank you for sending us feedback";
        }
      }
    }
    ?>
    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

使用这些

 From: <input type="email" name="email" required><br>
 Subject: <input type="text" name="subject" required><br>
 Message: <textarea rows="10" cols="40" name="message" required></textarea><br>

参考thisthis