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?

  • Does anyone know how to solve a problem? - Mykola Shkit

1 answer 1

Try this:

 $("#create_comment").submit( function(event) { var data = $(this).serialize(); var action = $(this).attr("action"); $.ajax( { url : action, type: "POST", data : data, success: function(data, textStatus, jqXHR) { }, error: function(jqXHR, textStatus, errorThrown) { } }); event.preventDefault(); event.unbind(); }); 

To call Widget from the controller, I do the following:

 class CafeController extends yii\web\Controller { public function actions() { return [ 'get-comment-widget' => CommentWidgetAction::class, ]; } } 

And Action :

 class CommentWidgetAction extends yii\base\Action { public function run() { return $this->controller->renderContent(DeleteDomain::widget()); } } 
  • and how to write the path to the widget controller? - Mykola Shkit
  • @MykolaShkit In terms of how to call the widget method? - NPreston
  • just in action I can only write the path to the controller / action, not the widget - Mykola Shkit
  • That is, go to ajax request to return the widget? - NPreston
  • Yes. I have a view widget loaded with a form and a list of comments already. I need to press the widget controller from the widget form - Mykola Shkit