V
V
Viktor Yanyshev2017-10-13 22:46:32
Yii
Viktor Yanyshev, 2017-10-13 22:46:32

Why does url change on Yii2 form submit if Pjax 'enablePushState' => false?

I have a widget to use it in the layout, because the form is end-to-end and there are also a couple of forms in a regular view.
So, in a normal view, the forms send an ajax request and do not change the current url, but the form from the widget sends an ajax request but changes the url.
Forms in the view:

<?
                    Modal::begin([
                        'header' => '<h3>Запись на консультацию</h3>',
                        'toggleButton' => [
                            'label' => 'Записать на консультацию',
                            'tag' => 'button',
                            'class' => 'btn btn__white',
                        ],
                    ]);

                    Pjax::begin(['enablePushState' => false]);
                        echo Html::beginForm(Url::toRoute('forms/writetoconsult'),'POST', ['data-pjax' => '']);

                            echo '<div class="form-group">';
                                echo Html::textInput('name', '', ['class' => 'form-control', 'placeholder' => 'Ваше имя']);
                            echo '</div>';

                            echo '<div class="form-group">';
                                echo Html::textInput('phone', '', ['class' => 'form-control', 'placeholder' => 'Ваш телефон']);
                            echo '</div>';

                            echo '<div class="form-group">';
                                echo Html::submitButton('Записаться', ['class' => 'btn block-center btn__border']);
                            echo '</div>';

                        echo Html::endForm();
                    Pjax::end();

                    Modal::end();


                    Modal::begin([
                        'header' => '<h3>Задать вопрос</h3>',
                        'toggleButton' => [
                            'label' => 'Задать вопрос',
                            'tag' => 'button',
                            'class' => 'btn btn__border-white pull-right'
                        ],
                    ]);

                    Pjax::begin(['enablePushState' => false]);
                       echo Html::beginForm(Url::toRoute('forms/questionspecialist'),'POST', ['data-pjax' => '']);

                            echo '<div class="form-group">';
                                echo Html::textInput('name', '', ['class' => 'form-control', 'placeholder' => 'Ваше имя']);
                            echo '</div>';

                            echo '<div class="form-group">';
                                echo Html::textInput('email', '', ['class' => 'form-control', 'placeholder' => 'Ваш email']);
                            echo '</div>';

                            echo '<div class="form-group">';
                                echo Html::textarea('email', '', ['class' => 'form-control', 'placeholder' => 'Ваш вопрос?', 'rows' => 5]);
                            echo '</div>';

                            echo '<div class="form-group">';
                                echo Html::submitButton('Записаться', ['class' => 'btn block-center btn__border']);
                            echo '</div>';

                       echo  Html::endForm();
                    Pjax::end();

                    Modal::end();
                ?>

Widget form:
<?
                Pjax::begin(['enablePushState' => false]);
                    echo Html::beginForm(Url::toRoute('forms/appointments'), 'POST', [
                        'data-pjax' => '',
                        'id' => 'appoint',
                        'class' => 'appointments__form form-horizontal',
                    ]);
            ?>

                <div class="col-md-6">
                    <div class="col-md-11 col-md-offset-1">
                        <div class="form-group">
                            <?= Html::textInput('name', '', ['class' => 'form-control', 'placeholder' => 'Ваше имя']) ?>
                        </div>
                        <div class="form-group">
                            <?= Html::textInput('phone', '', ['class' => 'form-control', 'placeholder' => 'Ваш номер телефона']) ?>
                        </div>
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="col-md-11 col-md-offset-1">
                        <div class="form-group">
                            <div class="form__select">
                                <?= Html::dropDownList('specialist', '', ArrayHelper::map($items, 'id','name'),
                                    ['prompt' => 'Выбериате специалиста','class' => 'form-control']); ?>
                            </div>
                        </div>
                    </div>
                    <?= Html::submitButton('Записаться', ['class' => 'btn btn-large btn__bg pull-right']) ?>
                </div>
                <div class="clearfix"></div>

                <?
                    Html::endForm();
                    Pjax::end();
                ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-10-15
@webinar

see how the requests differ.
try to manually set the id for pjax widgets.
check the timeout, because after it expires there is a normal request.
Check if the code is actually sending the data you expect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question