How to trigger form submit via Javascript? (for google recaptcha) #1660
Replies: 2 comments 1 reply
-
@ktross and I figured out a way to do it. Submit the form for validation, then fire off an event and listen for it in your script tag. This will set the recaptcha field so the rest can happen in a method called public function submit() {
$this->validate();
$this->emit('validation-passed');
} Livewire.on('validation-passed', event => {
grecaptcha.ready(function () {
grecaptcha.execute('YOUR-PUBLIC-KEY', {action: 'contact'})
.then(function (token) {
@this.set('recaptcha', token)
})
})
}) public function updateRecaptcha($token)
{
// do more recaptcha things here to validate against Google.
// process the rest of your form...
} |
Beta Was this translation helpful? Give feedback.
-
Heres how I did it using recaptcha v2. You need to You need to add some values to
My rule:
My component:
My view:
Of course, you'll need to change your properties if you want to use Hope this helps someone. |
Beta Was this translation helpful? Give feedback.
-
I need to execute some javascript code just before sending a form.
Problem is, if I do
document.getElementById('quick-register-form').submit();
then wire:submit isn't triggered, instead there's a regular form submit.How to trigger
wire:submit
via javascript?The following code is exactly as google suggests when using Recaptcha V3
https://developers.google.com/recaptcha/docs/v3#automatically_bind_the_challenge_to_a_button
Beta Was this translation helpful? Give feedback.
All reactions