I have a registration form with the new google recaptcha widget on it. When a user submits info, we check the validation with javascript and return the problems to the page
<script>
function cform(token) {
$.ajax({
type: 'POST',
url: 'contact_chk.php',
data: $('#cform').serialize(),
success: function(response) {
grecaptcha.reset();
$("#con_msg").html(response);
document.getElementById("name").value='';
document.getElementById("subject").value='';
document.getElementById("email").value='';
document.getElementById("phone").value='';
document.getElementById("message").value='';
},
error: function(response) {
grecaptcha.reset();
$("#con_msg").html('Try Again...');
}
});
}
</script>
<script src='https://www.google.com/recaptcha/api.js'></script>
I had same problem with excelwebzone/recaptcha-bundle
for Symfony2. Basicly is says that recaptcha was loaded in past to your DOM and placeholder that should be empty was full.
Those bundles dont have settings for explicit callbacks so you have to make sure that placeholder or even whole form is empty before you load recaptcha.
And mine was stuck in DOM cause i was loading it with AJAX. On second call it was there.
You can simply use $('#your-div-with-form').html('')
If you are using grecaptcha.render with jQuery, please ensure that you are actualy setting DOM element, instead of jQuery element:
This will work:
grecaptcha.render( $('.g-recaptcha')[0], { sitekey : 'your_key_here' });
but this wont:
grecaptcha.render( $('.g-recaptcha'), { sitekey : 'your_key_here' });
The method you are looking for is grecaptcha.reset()
However, in order for this function to work you have to render the reCaptacha explicitly like this : <script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
Try calling the reload()
function within ReCaptcha (version 1):
Recaptcha.reload();
How to Reload ReCaptcha using JavaScript?
I was able to do it just by using:
grecaptcha.reset();