Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a recaptcha error within the submitted form's page?

Tags:

html

php

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.

like image 327
user3403283 Avatar asked Mar 10 '14 19:03

user3403283


People also ask

Why is reCAPTCHA not showing?

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.

How do I view 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.

What is a reCAPTCHA validation error?

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.


1 Answers

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.

like image 123
aksu Avatar answered Sep 21 '22 08:09

aksu