Answer the question
In order to leave comments, you need to log in
Why are multiple ajax requests triggered on a single form submission?
Hello everyone, I have a modal in which I load data via ajax like this
$this->registerJs("
$('#add_report').on('click', function() {
$('#blockForLoadfunction').empty();
$('#blockForLoadfunction').load('" . Url::to(['/project-group-report/load']) . "');
});
");
Modal::begin([
'options' => [
'id' => 'kartik-modal',
'tabindex' => false
]
]);
Pjax::begin([
'id'=> 'reportModelShow',
'enablePushState'=> false,
'timeout'=>5000
]);
<?php $form = ActiveForm::begin([
'action' =>['/project-group-report/load'],
'id' => 'reportModelShowForm',
'options' => [
'enctype' => 'multipart/form-data',
'data-pjax' => true,
]
]); ?>
//разные поля формы
<?php ActiveForm::end(); ?>
Pjax::end();
Modal::end();
$this->registerJs("
$('#kartik-modal').modal('show');
$('.btn-outline, .close').on('click', function() {
$('#kartik-modal').modal('hide');
});
");
Answer the question
In order to leave comments, you need to log in
the number of ajax requests per submission will be the same as I closed/opened the modal
The fact is that by initializing PJAX on the form, the form submit handler is hung, it is delegated, that is, through $ (document). on (...), therefore, with each load, handlers are hung again and again, you can see the code in the developer panel, in the server response usually at the very end there is a block
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question