GetFlash + Yii2 submit via ajax?

Good day. Learning Yii2 and have affected the implementation of such things as comments.
Comments are added by pressing the "send" button, go to the admin area, where approved and fast.
The idea is that when you add a comment, I notice the user that the comment on the check, clears the text input field and everyone is happy. Now managed to implement sending the comment in the admin area only after a page refresh. I would like to do ajax.

Code review -
<?php if(!Yii::$app->user->isGuest):?>
 <div class="leave-comment">
 <?php if(Yii::$app->session->getFlash('comment')):?>
 <div class="alert alert-success" role="alert">
 <?= Yii::$app->session->getFlash('comment'); ?>
</div>
 <?php endif;?>

 <?php $form = \yii\widgets\ActiveForm::begin([
 'action'=>['site/comment', 'id'=>$article->id],
 'options'=>['class'=>'form-horizontal contact-form', 'role'=>'form']])?>

 <div class="form-group">
 <div class="col-md-12" style="padding: 0;">
 <?= $form->field($commentForm, 'comment')->textarea(['class'=>'form-control comment-form', 'placeholder'=>'Your message'])->label(false)?>
</div>
</div>

 <button type="submit" class="btn send-btn">Send</button>
 <?php \yii\widgets\ActiveForm::end();?>

</div>
 <?php endif;?>
April 19th 20 at 12:29
1 answer
April 19th 20 at 12:31
$this->registerJs(
<<<JS

 $('form').on('beforeSubmit',function(event) {
 var yiiForm = $(this); 
yiiForm.find('.has-error').removeClass('has-error');
yiiForm.find('.help-block').html(");

$.ajax({
 url: yiiForm.attr('action'),
 type: 'post',
 data: yiiForm.serialize(),
 success: function(result) {
 if(result.errors) {
 jQuery.each(result.errors, function(id, error) {
 $('.field-' + id).addClass('has-error').find('.help-block').html(error);
});
}

 if(result.status == true){
 // show message
}
 } 
});

 return false;

});
JS
 );

Find more questions by tags JavaScriptAJAX