Answer the question
In order to leave comments, you need to log in
Yii2 Pjax - how to send a form in a container to one action, and update the content from another?
I'm trying to make it concise and simple, with built-in tools.
There is such a form.
A comment should be sent to your controller and the plate will be updated
<?php Pjax::begin([
'id' => 'comments-pjax',
'enablePushState' => false,
'formSelector' => '#comments-form'
]); ?>
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Комментарии</h3>
</div>
<div class="box-body">
<?= GridView::widget([
...
]); ?>
</div>
<div class="box-footer">
<?php $form = ActiveForm::begin([
'id' => 'comments-form',
'action' => '/lead-comments/create?lead_id='.$model->id,
]); $comm = new LeadComments; ?>
<?= $form->field($comm, 'lead_id')->hiddenInput(['value' => $model->id])->label(false) ?>
<?= $form->field($comm, 'text')->textarea() ?>
<?= app\components\ViewHelper::saveButton(null, 'Добавить комментарий') ?>
<?php ActiveForm::end(); ?>
</div>
</div>
<?php Pjax::end(); ?>
Answer the question
In order to leave comments, you need to log in
you can pull the form outside the pjax container, send it with ajax, and reload pjax upon successful save.
$(document).on('beforeSubmit', '#my-form', function () {
var _this = $(this);
$.ajax({
url: _this.attr('action'),
data: _this.serialize(),
type: 'POST',
dataType: 'json',
success: function (response) {
if (response.success) {
$.pjax.reload({
container: "#my-pjax-container-id"
});
} else {
// alert
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.info(textStatus + ' ' + errorThrown);
}
});
return false;
});
It won't work, it's easier to make your bike. Tested by pain, suffering and wasted time. You can use pjax as well, but you can't do without js handles.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question