John Davidson

php - how to login after get key google recaptchaV3


I´m traying to do logging in my web with ajax. But i need generated google key when i do click in login, and before to do login. I have this function:

$('#main-login-form').on('submit', function(e){

let key = $("#recaptchaResponse").val();

if(key != null || key != ""){

var $this = $(this);


var data = $this.serializeArray();
data.push({name: 'currentName', value: config.url.currentName});

type: $this.attr('method'),
url: $this.attr('action'),
data: data,
dataType: $'type'),
success: function (response) {
if(response.success) {
$(location).attr('href', response.redirect);
error: function (jqXHR) {
var response = JSON.parse(jqXHR.responseText);

if (response.errors.password) {
} else if ( {
} else if (response.errors.gRecaptchaResponse) {

this function call to renewKeyRecaptcha(); that generated key, but i need to do click login twice and before i´m login... i need to do once click in login and generate key and login... But i can´t. I don´t know if i´m doing well or i would do to another way.

My function that generate key:

function renewKeyRecaptcha(){
// add key google to input
var key = config.url.recaptchaGoogle;
grecaptcha.ready(function() {
grecaptcha.execute(key, {action: 'login'}).then(function(token) {

this function run ok.

Thanks for help.

My Answer:

grecaptcha.ready(function() {
grecaptcha.execute('YOUR_SITE_KEY', {action: 'login'}).then(function(token) {
// Add the token to the form data

// Submit the form
url: 'login.php',
type: 'POST',
data: $('#loginForm').serialize(),
success: function(response){
// Handle the response from the server

In this code, replace 'YOUR_SITE_KEY' with your actual Google reCAPTCHA site key. This code will execute the reCAPTCHA v3 verification when the login form is submitted, add the token to the form data, and then submit the form via AJAX to the login.php file. You can then handle the response from the server in the success function.

Rate this post

3 of 5 based on 9786 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us