表单提交错误html& PHP

时间:2017-10-26 09:57:11

标签: php ajax html5

我正在使用ajax验证来填充php联系表单。当我提交联系表单时,我收到错误消息“垃圾邮件”,这是不应该的。

以下是我的代码html& php代码。

的index.php

<form id="contactform" method="post" action="mailer.php">
    <div class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" id="name" name="username" placeholder="Enter your name">
    </div>
    <div class="form-group">
        <label for="phone">Phone</label>
        <input type="text" class="form-control" id="phone" name="phone" placeholder="Enter your phone number">
    </div>
    <div class="form-group">
        <label for="email">Email</label>
        <input type="email" class="form-control" id="email" name="email" placeholder="Enter your email address">
    </div>
    <div class="form-group">
        <label for="message">Message</label>
        <textarea class="form-control" id="message" name="message" rows="4" placeholder="Enter your email message"></textarea>
    </div>

    <br>
    <div class="g-recaptcha" data-sitekey="6Lct9zUUAAAAABpj9vwKX9B7x_AH8s9gwJzFW1sB"></div>
    <br>
    <div id="formresult">
    </div>
    <button type="submit" name="submit" class="btn btn-info">Submit</button>
</form>

Mailer.php

  <?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $name = trim($_POST["username"]);
    $phone = trim($_POST["phone"]);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    if(isset($_POST['g-recaptcha-response'])){
      $captcha = $_POST['g-recaptcha-response'];
    }

    //Validate the data
    if (empty($name) OR empty($phone) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($captcha)) {
      http_response_code(400);
      echo "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span> <strong>Please fill all the form inputs and check the captcha to submit.</strong>";
      exit;
    }

    //recipient email address.
    $recipient = "abc@abc.com";
    $subject = "New message from $name";

    //email content.
    $email_content = "Name: $name\n";
    $email_content .= "Phone: $phone\n\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Message:\n$message\n";
    //email headers
    $email_headers = "From: $name <$email>";
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRATE_KEY&amp;response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
    $decoded_response = json_decode($response, true);

    if($decoded_response['success'] == true){
      // Send the email.
      if (mail($recipient, $subject, $email_content, $email_headers)) {
        http_response_code(200);
        echo "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span> <strong>Thank You! Your message has been sent.</strong>";
      } else {
        http_response_code(500);
        echo "Your message cannot be sent";
      }
    } else {
      http_response_code(400);
      echo 'spammer!';
    }
  }
  ?>

当我提交表单时,我收到错误消息“垃圾邮件发送者”,甚至邮件也无法通过。

我知道逻辑中存在一些错误,但我无法弄明白。

任何人都可以帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

代码行:

if($decoded_response['success'] == true){

必须评估为false,并执行该语句的else部分。显示垃圾邮件消息。

检查从recaptcha url返回的已解码内容,并将其分配给$ decoding_response变量。

print_r($decoded_response); // should show the whole contents