grav-plugin-form icon indicating copy to clipboard operation
grav-plugin-form copied to clipboard

AJAX forms do not work anymore with captcha

Open kaphadley opened this issue 3 years ago • 7 comments

Hi,

AJAX forms (as described in the documentation) do not work anymore when the form contains a type: captcha field.

Ajax and captcha work fine wight grav-plugin-form v3.0.8, but fails from at least v4.0.0.

kaphadley avatar Mar 14 '21 23:03 kaphadley

Same issue here.

marcocevoli avatar Mar 22 '21 21:03 marcocevoli

we'll take a look at this...

rhukster avatar Mar 22 '21 21:03 rhukster

This is due to the captcha integration bypassing our own event handlers by submitting the form. As a workaround, you can override the form's submit function. For instance, adapting the documentation:

const form = document.querySelector('#ajax-test-form');
form.submit = function() {

    const result = document.querySelector('#form-result');
    const action = form.getAttribute('action');
    const method = form.getAttribute('method');

    fetch(action, {
        method: method,
        body: new FormData(form)
    })
    .then(function(response) {
        if (response.ok) {
            return response.text();
        } else {
            return response.json();
        }
    })
    .then(function(output) {
        if (result) {
            result.innerHTML = output;
        }
    })
    .catch(function(error) {
        if (result) {
            result.innerHTML = 'Error: ' + error;
        }

        throw new Error(error);
    });
}


dledanseur avatar Apr 02 '21 09:04 dledanseur

@dledanseur your code is the same as the one from the documentation, except the missing event.preventDefault();. How is it supposed to solve the issue?

kaphadley avatar Apr 07 '21 10:04 kaphadley

@dledanseur your code is the same as the one from the documentation, except the missing event.preventDefault();. How is it supposed to solve the issue?

Not exactly the same, the key point here is to override the form's submit function with form.submit =

dledanseur avatar Apr 07 '21 16:04 dledanseur

Any news about this. We're getting loads of spam because we needed to disable the captcha...

marcocevoli avatar Aug 13 '21 14:08 marcocevoli

More than a year has passed since the last answer... Spam is a real problem. Any news? Moreover it doesn't work with dropzone

01Kuzma avatar Oct 03 '22 17:10 01Kuzma