Error message not displayed when Google ReCAPTCHA is blank

Hi there,

I have the following form which uses a Google ReCAPTCHA checkbox.

The issue I am having is that there is no message displayed when the checkbox is not selected, but at the same time, it displays the success message.

This is the form code I have:

<?php
/* [VERIFY CAPTCHA FIRST] */
$secret = 'SECRET'; // CHANGE THIS TO YOUR OWN!
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=".$_POST['g-recaptcha-response'];
$verify = json_decode(file_get_contents($url));

/* [PROCESS YOUR FORM] */
if ($verify->success) {
  $to = "ME@ME.com"; // CHANGE THIS TO YOUR OWN!
  $subject = "Contact Form";
  $message = "Name - " . $_POST['name'] . "\n";
  $message .= "Email - " . $_POST['email'] . "\n";
$message .= "Telephone - " . $_POST['telephone'] . "\n";
  $message .= "Message - " . $_POST['message'] . "\n";
  if (@mail($to, $subject, $message)) {
    // Send mail OK
    // @TODO - Show a nice thank you page or something
  } else {
    // Send mail error
    // @TODO - Ask user to retry or give alternative
  }
} else {
  // Invalid captcha
  // @TODO - Show error message, ask user to retry
}
if( isset($_POST['fromPerson']) )
{
     $fromPerson = '+from%3A'.$_POST['fromPerson'];
     echo $fromPerson;
}


?>

<script src='https://www.google.com/recaptcha/api.js'></script>
  <form  method="post" id="contact" class="contact-form">
    <label>Your name</label>
    <input type="text" name="name" required />
    <label>Your email address</label>
    <input type="email" name="email" required />
	  <label>Your telephone number</label>
    <input type="text" name="telephone" />
    <label>Your message</label>
    <textarea name="message" required></textarea>

    <!-- [PUT THE CAPTCHA WHERE YOU WANT IT] -->
	  <div class="captcha col-md-12 col-centered">
    <div class="g-recaptcha" data-sitekey="SITEKEY"></div>
	  </div>
    <input type="submit" value="Submit"/>
  </form>

and then on another page, I have this code which displays the success/message:

<?php if (count($_POST)>0) echo "<div class='submitted'>Thank you, we have received your message and will be in touch shortly.</div>"; ?>

Can anyone see what I have wrong?

Thanks!

I can’t - your layout is different than the one I had working. I used the data-callback parameter in the recaptcha div, and have that call my verification code which in turn returns the hidden information. That doesn’t get a callback until they’ve ticked the box, so there isn’t a way they can submit without ticking. You could perhaps do the same, by having some JS code that only displays the submit button when the verification is good.

I notice as well that at the start of your page you have code that calls the verification, but it doesn’t look to see whether the form has been submitted. Don’t you get errors using that approach?