I would like to submit data using Jquery and ajax and do some stuff.
Trying Google Recaptcha V3. In the following code, on the line the alert ("token 2 is " + Mytoken); ---> here i get an error saying Mytoken isn't defined
The thing is how do i get grecaptcha.execute to return data that I can use elsewhere? For example, if the captcha worked, then I submit some ajax data with the token code and on the submitted backend script, validate the token there.
<html>
<head>
<title>Recaptcha Ajax</title>
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo $captcha_site_key; ?>"></script>
</head>
<body>
<script>
var MYtoken;
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo $captcha_site_key; ?>', {action: 'test_page'}).then(function(token) {
//$("#pet_token").val(token);
//$('myform').prepend('<input type="hidden" name="recaptchaTOKEN" value="' + token + '">');
alert ("token is " + token);
alert ("and Mytoken is " + token);
});
});
for (var i = 1; i <= 5; i++) {
var tick = function(i) {
return function() {
console.log(i);
}
};
setTimeout(tick(i), 1000 * i);
}
alert ("token 2 is " + Mytoken);
</script>