php验证联系表单问题

时间:2018-04-12 10:12:46

标签: php forms validation web contact

我在验证简单的联系表单(姓名,电子邮件,消息)方面遇到了麻烦。我已经设法使用PHP实际将消息发送到我的电子邮件,但是您只需输入随机单词即可发送电子邮件。没有办法过滤这个,我希望输入有效的电子邮件而不仅仅是乱码。我不太确定我做错了什么,因为我对PHP很新。另外一种可能阻止垃圾邮件/被黑客入侵的方法也很棒。

<?php


if ($_SERVER["REQUEST_METHOD"] == 'submit') {
  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['message'])) {
    $message = "Message is required";
  } else {
    $message = test_input($_POST['message']);
  }

}

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

if (isset($_POST['submit'])) {
   $name = $_POST['name'];
   $subject = $_POST['subject'];
   $emailFrom = $_POST['email'];
   $message = $_POST['message'];

   $emailTo = "domain@domain.com.au";
   $headers = "From: ".$emailFrom;
   $txt = "You have received an email from ".$name.".\n\n".$message;


   mail($emailTo, $subject, $txt, $headers);
   header("Location: index.php?mailsent");

}

?>

和HTML。

<form class="contact-form" action="contactform.php" method="post">
        <div class="row">
           <div class="col span-1-of-3">
              <label for="name">Name</label>
           </div>

           <div class="col span-2-of-3">
              <input type="text" name="name" placeholder="Your name" required>
           </div>
        </div>

        <div class="row">
           <div class="col span-1-of-3">
              <label for="email">Email</label>
           </div>

           <div class="col span-2-of-3">
              <input type="text" name="email" placeholder="Your e-mail" required>
           </div>
        </div>

        <div class="row">
           <div class="col span-1-of-3">
              <label for="subject">Subject</label>
           </div>

           <div class="col span-2-of-3">
        <input type="text" name="subject" placeholder="Subject">
           </div>
        </div>

        <div class="row">
           <div class="col span-1-of-3">
              <label>Message</label>
           </div>

           <div class="col span-2-of-3">
              <textarea name="message" placeholder="Message"></textarea>

              <button type="submit" name="submit">Send Mail</button>
           </div>
        </div>
     </form>

如上所述,我可以成功向自己发送电子邮件,但验证无效。点击提交后,它只显示php代码。

任何帮助都非常感谢,如果你能提出一些好的资源/书籍来学习php,那也很棒。

谢谢!

1 个答案:

答案 0 :(得分:1)

将您的HTML更改为此

  

<input type="email" name="email" placeholder="Your e-mail" required>

我认为它的工作