Zack Tarr
Zack Tarr

Reputation: 119

No recaptcha in POST var being sent

The code below is the code for my form on my webpage, then test.php. The issue is there is no recaptcha var being sent in the $_POST. I have ensured the tag for the recaptcha is with in the form tag, the call to googles .js is in the head. I cannot find the reason my form is not sending the 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>';
    }
?>

Upvotes: 0

Views: 163

Answers (1)

robere2
robere2

Reputation: 1716

After looking at your website that you provided in the comments, I noticed your submit form handler is only submitting 4 fields (name, email, phone, and comments). This needs to be modified to include g-recaptcha-response.

From http://zacktarrcreations.com/AFK/js/plugins.js on line 1141:

$('#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;

    });

Change to:

$('#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;

    });

Tested and it worked for me.

Upvotes: 1

Related Questions