PHP联系表格提交(蜜罐)

时间:2016-10-30 17:50:32

标签: javascript php jquery html forms

PHP新手因此是一个非常基本的问题。我正在尝试创建一个内置Honey Pot属性的联系表单。我想在提交之前检查JS和PHP中的这个蜜罐值。

例:

  • 如果没有填写任何字段,则JS错误不提交
  • 如果所有字段都已填写但是Honey Pot,通过JS,POST到PHP并发送电子邮件
  • 如果JS上的所有字段都填写失败并且没有POST,那么我希望这也适用于PHP,即使为了更好地理解PHP也是多余的。

问题:

JS验证很好,我检查输入字段的.val。但是,我似乎无法在PHP中获得相同的验证工作。

index.html(联系表单)*使用jQuery Validate:

<form id="contactForm" method="GET" action="" class="contact-form" novalidate="novalidate">
  <div class="input-field col s12">
    <input name="name" type="text" id="name" class="validate" required/>
    <label for="name">Name</label>
  </div>
  <div class="input-field col s12">
    <input name="email" type="text" id="email" class="validate" required/>
    <label for="email">E-mail</label>
  </div>
  <div class="input-field col s12">
    <textarea name="message" id="message" class="materialize-textarea" required></textarea>
    <label>Message</label>
   </div>
   <div class="input-field comment col s12">
     <label>If you're human leave this blank:</label>
     <input name="comment" type="text" id="comment" class="form-comment" />
   </div>
   <div class="col s12">
     <button class="btn button-primary contact-submit-button waves-effect waves-light" type="submit">Send</button>
   </div>
   <div id="form-result"></div>
 </form>

与-form.js

$(document).ready(function () {
  var contactFormResult = $('#form-result');
  var contactForm = $('#contactForm');
  contactFormResult.hide();
  $.validator.setDefaults({
    submitHandler: function () {
      var nameValue = $('#name').val();
      var emailValue = $('#email').val();
      var messageValue = $('#message').val();
      var robotValue = $('#comment').val();
      console.log('contact form submission, name value: ' + nameValue, ' email value: ' + emailValue + ' message value: ' + messageValue + ' robot value: ' + robotValue);

      // 
      // I want this to pass through JS and FAIL in PHP
      //
      if (robotValue = ''){
        contactFormResult.text('you are a robot').fadeIn();
        return false;
      } else {
         $.ajax({
          type: "POST",
          url: "contactSubmission.php",
          data: {
            name: nameValue,
            email: emailValue,
            message: messageValue,
            robotTest: robotValue
          }
        }).done(function () {
          contactForm.find("input[type=text], input[type=email], textarea").val("");
          contactFormResult.text('form submitted successfully').fadeIn();
        });
      }
    }
  });
  contactForm.validate();
});

contactSubmission.php

<?php
if ($_POST){
    //Sanitize input data using PHP filter_var().
    $nameValue = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
    $emailValue = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $messageValue = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
    $robotValue = filter_var($_POST["robotTest"], FILTER_SANITIZE_STRING);

    if(!empty($robotValue){
        die();
    } else {
        $to = 'myemail@gmail.com';
        $subject = 'Subject Line';

        $message = 'Name: ' .$nameValue. ' Email: ' .$emailValue.' Message: '.$messageValue;

        mail($to, $subject, $message);
    }
}
?>

0 个答案:

没有答案