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!