Am trying to add google recaptcha 3 to my html form. Works find when i call it on page load but due to the fact that it expires after 2mins and should be called when ready, am trying to add it to my submit function but don't know where am doing it wrong.
my js code is:
<script>
$(document).ready(function () {
$('.regformsubmit').click(function (e) {
grecaptcha.execute('6LcVzjYcmykey is here', {action: 'validate_captcha'}).then(function(token) {
// add token to form
$("#register_form").prepend('<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response" value="' + token + '">');
});
var myForm = jQuery( "#register_form" );
if ( ! myForm[0].checkValidity() )
{
// bonk! failed to validate, so return true which lets the
// browser show native validation messages to the user
return true;
}
e.preventDefault();
var country = $('#country').val();
var fname = $('#fname').val();
var lname = $('#lname').val();
var emailid = $('#emailid').val();
var password = $('#password').val();
var confirm_password = $('#confirm_password').val();
var phone = $('#phone').val();
var vcode = $('#vcode').val();
var upline = $('#upline').val();
var agree_term = $('#agree_term').val();
var cr = $('#g-recaptcha-response').val();
$.ajax
({
type: "POST",
url: "process-reg.php",
data: { "country": country,"fname":fname, "lname": lname, "emailid": emailid, "password": password, "confirm_password": confirm_password, "phone": phone, "vcode": vcode, "upline": upline, "agree_term": agree_term, "cr": cr },
success: function (data) {
$('#RegResult').html(data);
$('#register_form')[0].reset();
}
});
});
});
</script>