Implement the new Invisible reCaptcha from Google
Invisible reCAPTCHA
Implementing Google's new Invisible reCAPTCHA is very similar to how we add v2 to our site. You may add it as its own container like normal, or the new method of adding it to the form submit button. I hope this guide will help you along the correct path.
Standalone CAPTCHA Container
Implementing recaptcha requires a few things:
- Sitekey
- Class
- Callback
- Bind
This will be your final goal.
<div class="g-recaptcha" data-sitekey="<sitekey>"
data-bind="recaptcha-submit" data-callback="submitForm">
</div>
When using the standalone method, you must have data-bind set to the ID of your submit button. If you do not have this set, your captcha will not be invisible.
A callback must also be used to submit the form. An invisible captcha will cancel all events from the submit button, so you need the callback to actually pass the submission on.
<script>
function submitForm() {
var form = document.getElementById("ContactForm");
if (validate_form(form)) {
form.submit();
} else {
grecaptcha.reset();
}
}
</script>
Notice in the example callback that there is also custom form validation. It is very important that you reset the reCAPTCHA when the validation fails, otherwise you will not be able to re-submit the form until the CAPTCHA expires.
Invisible CAPTCHA Button
A lot of this is the same as with the standalone CAPTCHA above, but instead of having a container, everything is placed on the submit button.
This will be your goal.
<button class="g-recaptcha" data-sitekey="<sitekey>"
data-callback="submitForm" data-badge="inline" type="submit">
Submit</button>
There's something new here, data-badge. This is a div that gets inserted into the DOM that contains the inputs required for reCAPTCHA to function. It has three possible values: bottomleft, bottomright, inline. Inline will make it display directly above the submit button, and allow you to control how you would like it to be styled.
On to your question
<form action="test.php" method="POST" id="theForm">
<script>
function submitForm() {
document.getElementById("theForm").submit();
}
</script>
<input type="text" name="email" placeholder="Email">
<input type="password" name="password" placeholder="Password">
<div class="g-recaptcha" data-sitekey="<sitekey>" data-bind="recaptcha-submit" data-callback="submitForm"></div>
<input type="submit" name="login" class="loginmodal-submit" id="recaptcha-submit" value="Login">
</form>
Or
<form action="test.php" method="POST" id="theForm">
<script>
function submitForm() {
document.getElementById("theForm").submit();
}
</script>
<input type="text" name="email" placeholder="Email">
<input type="password" name="password" placeholder="Password">
<button class="loginmodal-submit g-recaptcha" data-sitekey="<sitekey>" data-callback="submitForm" data-badge="inline" type="submit" value="Login">Submit</button>
</form>
I hope this helps you and future coders. I'll keep this up-to-date as the technology evolves.
Here is how to implement a client + server side (php) Invisible reCaptcha functionality:
- Client Side
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>reCaptcha</title>
<!--api link-->
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<!--call back function-->
<script>
function onSubmit(token) {
document.getElementById('reCaptchaForm').submit();
}
</script>
</head>
<body>
<div class="container">
<form id="reCaptchaForm" action="signup.php" method="POST">
<input type="text" placeholder="type anything">
<!--Invisible reCaptcha configuration-->
<button class="g-recaptcha" data-sitekey="<your site key>" data-callback='onSubmit'>Submit</button>
<br/>
</form>
</div>
</body>
</html>
- Server Side validation: Create a signup.php file
<?php
//only run when form is submitted
if(isset($_POST['g-recaptcha-response'])) {
$secretKey = '<your secret key>';
$response = $_POST['g-recaptcha-response'];
$remoteIp = $_SERVER['REMOTE_ADDR'];
$reCaptchaValidationUrl = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$response&remoteip=$remoteIp");
$result = json_decode($reCaptchaValidationUrl, TRUE);
//get response along side with all results
print_r($result);
if($result['success'] == 1) {
//True - What happens when user is verified
$userMessage = '<div>Success: you\'ve made it :)</div>';
} else {
//False - What happens when user is not verified
$userMessage = '<div>Fail: please try again :(</div>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>reCAPTCHA Response</title>
</head>
<body>
<?php
if(!empty($userMessage)) {
echo $userMessage;
}
?>
</body>
</html>