Home > Article > Backend Development > yii2 ActiveForm 表单怎么ajax提交
yii2 ActiveForm 有什么方法可以直接ajax提交?而不是自己用jq写代码去提交
<code> //view的代码 <?php $form = ActiveForm::begin([ 'action' => ['/admin/admin/adminadd'], 'id' => 'login-form', 'options' => ['class' => 'form-horizontal'], 'fieldConfig' => [ 'template' => "{label}\n<div class='\"col-lg-5\"'>{input}</div>\n<div class='\"col-lg-5\"'>{error}</div>", 'labelOptions' => ['class' => 'col-lg-2 control-label'], ], ]); ?> = $form->field($Admin, 'username');?> = $form->field($Admin, 'password')->passwordInput();?> = Html::submitButton('添加',['class' => 'btn btn-success']); ?> <?php ActiveForm::end(); ?> </code>
还有提交完,控制器里面怎么接收?
yii2 ActiveForm 有什么方法可以直接ajax提交?而不是自己用jq写代码去提交
<code> //view的代码 <?php $form = ActiveForm::begin([ 'action' => ['/admin/admin/adminadd'], 'id' => 'login-form', 'options' => ['class' => 'form-horizontal'], 'fieldConfig' => [ 'template' => "{label}\n<div class='\"col-lg-5\"'>{input}</div>\n<div class='\"col-lg-5\"'>{error}</div>", 'labelOptions' => ['class' => 'col-lg-2 control-label'], ], ]); ?> = $form->field($Admin, 'username');?> = $form->field($Admin, 'password')->passwordInput();?> = Html::submitButton('添加',['class' => 'btn btn-success']); ?> <?php ActiveForm::end(); ?> </code>
还有提交完,控制器里面怎么接收?
我是这么做的:https://github.com/lichunqiang/yii2-ajaxform
yii2封装了ajax + pushstate,在Pjax类中,你直接使用就行了哦
Pjax::begin();
中间为你的ActiveForm
Pjax::end();
这样不仅仅是ajax提交form,url还会刷新,比你自己实现ajax要好得多吧,还简单。