Фрагмент view:
Код: Выделить всё
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$this->title = 'Test';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-test">
<h1><?= Html::encode($this->title) ?></h1>
<h5><?= Html::encode('items ='.$items) ?></h5>
<?php $form = ActiveForm::begin([
'id' => 'test-form',
'layout' => 'horizontal',
]); ?>
<?= $form->field($model, 'test')->input('Категории') ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-primary',]) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Код: Выделить всё
$('#test-form').on('beforeSubmit',function() {
var data = $(this).serialize();
$.ajax ({
type: "POST",
url: "/site/test",
data: data,
success: function(res) {
console.log(res);
},
error: function() {
alert('error');
}
});
return false;
});
Код: Выделить всё
public function actionTest()
{
$model = new TestForm();
if (Yii::$app->request->isAjax) {
$items = "isAjax";
}
if ($model->load(Yii::$app->request->post()) && $model->test()) {
$items = "Form";
}
return $this->render('test', [
'model' => $model,
'items' => $items,
]);
}