<?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;?>
$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