A
A
AlexSer2022-04-07 11:24:21
Yii
AlexSer, 2022-04-07 11:24:21

Why is the form being double submitted?

Hello to all! For some reason, at the first click on the Submit form, $.post is sent twice.
Yii2 form code in modal window:

<?php
            Modal::begin([
                'header' => '<h3>Регистрация организации:</h3>',
                'size'=>'modal-lg',
                'toggleButton' => [
                    'label' => 'Регистрация организации',
                    'tag' => 'button',
                    'class' => 'btn btn-default',
                ],

            ]);
            $model=new \common\models\Organization();
            $form = ActiveForm::begin([
                'id'=>'form-org',
            ]);
            echo $form->field($model, 'title')->textInput()->label('Наименование');
            {?>
                <div class="row">
                    <div class="col-xs-4"><?=$form->field($model, 'inn')->textInput()->label('ИНН');?></div>
                    <div class="col-xs-4"><?=$form->field($model, 'ogrn')->textInput()->label('ОГРН');?></div>
                    <div class="col-xs-4"><?=$form->field($model, 'kpp')->textInput()->label('КПП');?></div>
                </div>
                <div class="row">
                    <div class="col-xs-6"><?=$form->field($model, 'telephone')->textInput()->label('Телефон');?></div>
                </div>
                <div class="row">
                    <div class="col-xs-12"><?=$form->field($model, 'address')->textarea()->label('Адрес');?></div>
                </div>
                <div class='modal-footer'><?=\yii\helpers\Html::submitButton('Сохранить',['class'=>'btn btn-default'])?></div>
            <?}

            ActiveForm::end();
            Modal::end();


jquery code:
$("#form-org").submit(function(event){
     $.post(
               'guide/add-organziation',
                $(this).serialize(),
                function(msg) { // получен ответ сервера  
                    console.log(msg);
                 }
            );
      return false;
    });

And double sending only at the first click.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Kinash, 2022-04-07
@nkinash

Paste in your code before sending a POST request
event.preventDefault();

$("#form-org").submit(function(event){
     event.preventDefault();
     $.post(
               'guide/add-organziation',
                $(this).serialize(),
                function(msg) { // получен ответ сервера  
                    console.log(msg);
                 }
            );
      return false;
    });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question