J
J
jekahm2016-08-31 14:55:29
JavaScript
jekahm, 2016-08-31 14:55:29

Why does the form's submit event occur twice in Yii2?

Good day!
To intercept the form submit event I use the following. code (as a test):

$(document).on("beforeSubmit", "#pm-form", function (event, messages) {
    var formdata = $(this).serialize();
    console.log(formdata);
    $(this).find(':submit').attr('disabled', true);
    return false;
  });

But, despite the fact that at the same time I make the submit button inactive, in the console you can see that the form is submitted at least twice when the button is quickly clicked. That is, in fact, the call to the beforeSubmit event occurs with a delay.
With what it can be connected? And is it possible to somehow fix it?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-08-31
@jekahm

That is, in fact, the call to the beforeSubmit event occurs with a delay.
With what it can be connected? And is it possible to somehow fix it?

beforeSubmit is just one of the events called during form submission in yii2. The user presses the submit button - the js form is validated, and only after it has been successfully completed, the beforeSubmit event is generated. Accordingly, validation also takes some time, and this leads to such a result. If you want to block the submit button, do it when the beforeValidate event occurs

A
Abdula Magomedov, 2016-08-31
@Avarskiy

I am using the normal js submit event. Everything works fine.

Note tip: Use a FormData object instead of a serialized string to submit the form.
More details here: https://learn.javascript.ru/xhr-forms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question