Hello. It is necessary to send the form data ajax. The view widget is loaded in the view.
<div role="tabpanel" class="tab-pane" id="reviews"> <?=\frontend\widgets\CommentWidget::widget(array('from'=>$result['id'])); ?> </div> Widget controller
namespace frontend\widgets; use app\models\Comment; use common\models\User; use yii\bootstrap\Widget; use yii\db\Query; class CommentWidget extends Widget { private $name; public $from; public $params = array(); public function init() { $user = new User(); $this->name = $user->getUserName(); } public function run() { $model = new Comment(); $temp = new Query(); $list = $temp->from('comment')->where(['from'=>$this->from])->all(); return $this->render('CreateForm', ['model' => $model, 'list' => $list], $this->from); } } Widget view:
<?php $form = \yii\bootstrap\ActiveForm::begin([ 'id' => 'create_comment', 'action' => \yii\helpers\Url::toRoute('/cafe/view'), 'enableClientValidation' => true, 'enableAjaxValidation' => true, 'validateOnBlur' => true, 'validateOnChange' => true, //'validationUrl' => \yii\helpers\Url::toRoute('cafe/view') ]); ?> <?=$form->field($model, 'text')->textarea(); ?> <?php echo \yii\helpers\Html::submitButton('Опубликовать'); ?> <?php if(empty($list)) : ?> <h3>Коментариев нет</h3> <?php else : ?> <?php foreach ($list as $value) : ?> <div class="result"> <p><?=$value['entity']?></p> <p>Текст сообщения: <?=$value['text']?></p> </div> <?php endforeach; ?> <?php endif; ?> <?php $form = \yii\bootstrap\ActiveForm::end(); ?> <script type="text/javascript"> $('#create_comment').on('beforeSubmit\',function(event, jrXHR, settings) { var form = $(this); $.ajax({ url: "/cafe/view", type: "get", data: form.serialize(), success:function(data) { alert("fsdgdfgdf"); }, error: function(data){ $('.result').html("Error"); } }); ); </script> How to make the data be sent via ajax?