POST var 中没有 recaptcha 被发送

No recaptcha in POST var being sent

下面的代码是我网页上的表单代码,然后test.php。问题是 $_POST 中没有发送 recaptcha var。我确保 recaptcha 的标签在表单标签中,对 googles .js 的调用在头部。我找不到我的表单不发送 var 的原因。

<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>


<div id="contact" class="form-contain">
    <fieldset>
        <div id="message"></div>
            <form method="post" action="js/test.php" name="contactform" id="contactform">
                 <div class="form-group">
                      <input name="name" id="name" type="text" value="" placeholder="Name" class="form-control" />
                 </div>
                 <div class="form-group">
                     <input name="email" id="email" type="text" value="" placeholder="Email" class="form-control" />
                 </div>
                 <div class="form-group">
                     <input name="phone" id="phone" type="text" value="" placeholder="Phone" class="form-control" />
                 </div>

                 <div class="g-recaptcha" data-sitekey="6LcMRQ0UAAAAACB1GVYh0oIQezzFcNmpsy0a7Sqx"></div>
                  <div class="form-group">
                      <button class="btn btn-primary" type="submit" id="cf-submit" name="submit">Send</button>
                  </div>
             </form>

        </fieldset>
    </div>

test.php:

<?php
    $email;$comment;$captcha;
    if(isset($_POST['email'])){
      $email=$_POST['email'];
    }if(isset($_POST['comment'])){
      $email=$_POST['comment'];
    }if(isset($_POST['g-recaptcha-response'])){
      $captcha=$_POST['g-recaptcha-response'];
    }
    if(!$captcha){
      echo '<h2>Please check the the captcha form.</h2>';
      exit;
    }
    $secretKey = "Put your secret key here";
    $ip = $_SERVER['REMOTE_ADDR'];
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
    $responseKeys = json_decode($response,true);
    if(intval($responseKeys["success"]) !== 1) {
      echo '<h2>You are spammer ! Get the @$%K out</h2>';
    } else {
      echo '<h2>Thanks for posting comment.</h2>';
    }
?>

查看您在评论中提供的网站后,我注意到您的提交表单处理程序仅提交 4 个字段(nameemailphonecomments).这需要修改为包括 g-recaptcha-response.

来自第 1141 行的 http://zacktarrcreations.com/AFK/js/plugins.js

$('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(500,function() {
            $('#message').hide();

            $.post(action, {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    phone: $('#phone').val(),
                    comments: $('#comments').val(),
                },
                function(data){
                    document.getElementById('message').innerHTML = data;
                    $('#message').slideDown('slow');
                    $('#submit').removeAttr('disabled');
                    if(data.match('success') != null) $('#contactform').slideUp('slow');

                }
            );

        });

        return false;

    });

更改为:

$('#contactform').submit(function(){

        var action = $(this).attr('action');

        $("#message").slideUp(500,function() {
            $('#message').hide();

            $.post(action, {
                    name: $('#name').val(),
                    email: $('#email').val(),
                    phone: $('#phone').val(),
                    comments: $('#comments').val(),
                    "g-recaptcha-response": $('#g-recaptcha-response').val(),
                },
                function(data){
                    document.getElementById('message').innerHTML = data;
                    $('#message').slideDown('slow');
                    $('#submit').removeAttr('disabled');
                    if(data.match('success') != null) $('#contactform').slideUp('slow');

                }
            );

        });

        return false;

    });

经过测试,对我有用。