Ajax form representing twice Yii 2

I looked around, and none of the other similar recordings helped me. I created an AJAx form in Yii 2 and jQuery , and it seems like it submits the form twice.

My form:

 $form = ActiveForm::begin([ 'id' => 'company_form', 'ajaxDataType' => 'json', 'ajaxParam' => 'ajax', 'enableClientValidation' => false ]); 

My JS code is:

 $(document).ready(function() { /* Processes the company signup request */ $('#company_form').submit(function() { signup('company'); return false; }); }) function signup(type) { var url; // Set file to get results from.. switch (type) { case 'company': url = '/site/company-signup'; break; case 'client': url = '/site/client-signup'; break; } // Set parameters var dataObject = $('#company_form').serialize(); // Run request getAjaxData(url, dataObject, 'POST', 'json') .done(function(response) { //......... }) .fail(function() { //..... }); // End } 

Should I stop the standard submit by putting return: false; into javascript code?

Why is he sent twice?

Additional information: However, a strange thing that appears only for the first time; if I send the message again, it is sent only once; but if I reload the page and click send, it will repeat this twice.

+5
source share
1 answer

You may need to change your code as shown below:

 $('#company_form').submit(function(e) { e.preventDefault(); e.stopImmediatePropagation(); signup('company'); return false; }); 

http://api.jquery.com/event.stoppropagation/

http://api.jquery.com/event.stopimmediatepropagation/

+7
source

Source: https://habr.com/ru/post/1207837/


All Articles