0

I have set up the google reCaptcha PHP plugin on this site:

http://benliger.webatu.com/

You can see it displays fine under Contact, however after adding in the necessary PHP into my form_process file the form still submits regardless of whether or not the reCaptcha is filled out. Here is my PHP code that sits in the form_process file:

 <?php

    //Check if POST data is set, and not empty, else it will do this every single time, submitted or not

    require_once('recaptchalib.php');
      $privatekey = "6Le2a_oSAAAAAJ81_yQvCelFMIHiUcG_k6u0S1fd";
      $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);

      if (!$resp->is_valid) {
        // What happens when the CAPTCHA was entered incorrectly
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
             "(reCAPTCHA said: " . $resp->error . ")");
      } else {



    if(isset($_POST) && !empty($_POST))
    {

        $mail_to = '[email protected]'; // specify your email here

        // Assigning data from the $_POST array to variables
        $name = $_POST['sender_name'];
        $mail_from = $_POST['sender_email'];
        $phone = $_POST['sender_phone'];
        $message = $_POST['sender_message'];

        // Construct email subject
        $subject = 'enquiry ' . $name;

        // Construct email body
        $body_message = 'From: ' . $name . "\r\n";
        $body_message .= 'E-mail: ' . $mail_from . "\r\n";
        $body_message .= 'Phone: ' . $phone . "\r\n";
        $body_message .= 'Message: ' . $message;

        // Construct email headers
        $headers = 'From: ' . $mail_from . "\r\n";
        $headers .= 'Reply-To: ' . $mail_from . "\r\n";

        $mail_sent = mail($mail_to, $subject, $body_message, $headers);

        if ($mail_sent == true){ 
            //Echo the message now, because it will be catched in your jQuery listerener (see code below)
            echo 'Thanks for getting in touch!';




         } else { 
            //Echo the message now, because it will be catched in your jQuery listerener (see code below)
            echo 'Message not sent :( Please, get in contact with me directly: [email protected]';
        }
        //This exit; is important, else the alert box will be full of the further html code
        exit;

    }
    }
    ?>

And my HTML:

<form name="myForm" action="form_process.php" method="POST"> 

 <?php
          require_once('recaptchalib.php');
          $publickey = "6Le2a_oSAAAAAEHu4u35QWlLzxzCYB1JnhFoI0u5"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>

.... and the rest of my form here

Might be worth noting my send button looks like so:

<input class="sendbutton" type="submit" name="send_message" value="Send"> 

Javascript:

$(document).on('submit','form',function(e){
    //Prevent the default action
    e.preventDefault();

var form = $(this);
    //Create an array of input values
    var data = $(this).serializeArray();
    //Do the ajax request
    $.post('form_process.php',data,function(responseMessage){
resetForm(form); 
        //Alert your message
$( ".mycontactform" ).html('<p>Thanks for getting in touchaaa!</p>');

        //alert(responseMessage);
     });

 });
14
  • Are you using the right key when generating? I can't find the problem either. Commented Oct 3, 2014 at 10:51
  • Try changing require_once to require. Commented Oct 3, 2014 at 10:55
  • 2
    You’re checking the CAPTCHA for validity before you even check whether the request was actually made via POST … that makes little sense. Commented Oct 3, 2014 at 10:59
  • 2
    Could you print_r the output of $resp? Commented Oct 3, 2014 at 10:59
  • I tried that Imran and no luck, as you can tell im pretty new to PHP so not entirely sure if the logic is correct, just tried to best follow instructions from googles documentation. @CBroe could you please elaborate on how it should look? Commented Oct 3, 2014 at 11:02

1 Answer 1

2

Error is in your jquery code .whatever the response is you are replacing it with <p>Thanks for getting in touchaaa!</p>

use alert(responseMessage) to see the response

so replace $( ".mycontactform" ).html('<p>Thanks for getting in touchaaa!</p>');

with alert(responseMessage)

Sign up to request clarification or add additional context in comments.

4 Comments

Solved it! My only question now is could I have it so that when the form is submitted WITH Captcha filled out properly it will echo the message to the html like i had before rather than through the alert javascript box?
$( ".mycontactform" ).html(responseMessage);
one last question, dont suppose theres a way to make the reponse message from the failed form come up in alert(responseMessage) and the successfull one to post in html like your suggestion is there?
make the response array on server and return as json by function json _dump(array) and in jquery function check for corresponding element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.