0

I am trying to set the form that user cannot submit if he has not checked the captcha. Captcha must be mandatory but i am unable to do it. Any help will he highly appreciated . My piece of code is :

<form action="#" method="post">
                        <input type="email" name="email" placeholder="E-mail" required="">
                        <input type="password" name="password" placeholder="Password" required="">
                                <div align="center" class="g-recaptcha" data-sitekey="6LdtPmUUAAAAAFmYGrd_JI5k_bNzJD9kUNsv4zS0"
                                style="transform:scale(1);-webkit-transform:scale(1);transform-origin:0 0;-webkit-transform-origin:0 0;"></div>

                        <div class="tp">
                            <input type="submit" name="submit" value="Login">
                        </div>
                    </form>

The is code after submitting

if(isset($_POST['submit'])
 { 
    $email = $_POST['email'];
    $secret = "6LdtPmUUAAAAAKf_wtXpvAhHzwbG9bY-xO_-V0a-";
    $ip = $_SERVER['REMOTE_ADDR'];
    $captcha = $_POST['g-recaptcha-response'];
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$captcha&remoteip$ip");
    $response = json_decode($response);
    if($response->success)
        echo "Verification Success. Your name is : $email";
    else
        echo "Verification failed.";


 }

?>
Ali Tahir
  • 31
  • 1
  • 7
  • Maybe the following topic will help you : https://stackoverflow.com/questions/27706594/how-can-i-make-recaptcha-a-required-field – Paul92ilm Jul 23 '18 at 07:42

1 Answers1

0

Need to use JavaScript to restrict user to submit form if captcha not verified. First add onsubmit attr to form as:

<form action="#" method="post" onsubmit="return check();">

On submit in JavaScript, you can check it as:

function check() {
    if (grecaptcha.getResponse() == "") {
        alert("Please verify captcha details.");
            return false;
    }
    return true;
}
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36