I am using the recaptcha widget on a contact form, i was wondering how would you be able to display the error the user would gets on the same page instead of going to another page and displaying the error.
Help for reCAPTCHA usersMake sure your browser is fully updated (see minimum browser requirements) Check that JavaScript is enabled in your browser. Try disabling plugins that might conflict with reCAPTCHA.
Click the pencil icon or Edit on the form or newsletter block. In the Storage tab, click Google reCAPTCHA. Switch the Use Google reCAPTCHA toggle on. Repeat these steps for all form blocks on your site where you want to add a reCAPTCHA.
reCAPTCHA analyzes interactions with the website to detect if they are made by a human or some form of automated abuse. Sometimes, you may see a "failed reCAPTCHA check" error message while trying to create or amend your account. This means the website believes your actions may be those of a bot.
You can put your form to submit on the same page, and check the post data on PHP side. Check this example:
<?php
$captcha = $_POST['captcha'];
if(isset($captcha)) {
$captcha_answer = // you need to put your correct captcha answer here
if($captcha == $captcha_answer) {
// captcha is correct, continue.
}
else {
// captcha is not correct, display error message.
echo '<div style="background-color:red; padding:3px;">Your captcha is not correct, please try again.</div>';
echo '<form method="post">
<input type="text" name="captcha" value="Type captcha here" />
</form>'; // print the page again
}
}
else {
?>
<form method="post">
<input type="text" name="captcha" value="Type captcha here" />
</form>
<?php } ?>
Another option is to use JavaScript with AJAX.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With