yii2 表单 $form
<?= $form->field($userRole, 'userid', ['options' =>['class' => 'bigDiv'] ])->textInput(['placeholder'=> 'ccc', 'class'=> 'inputSelf', 'maxlength' => true, 'value' => $user->id]) ?>
<?php
$form = ActiveForm::begin(['action' => ['sykc','id' => $model->id],
'method'=>'post',
'options' => ['id'=>'w-sykc-form','enctype' => 'multipart/form-data','autocomplete'=>"off",'data-pjax'=>'true'],
'fieldConfig' => [
'template' => '{label}<div class="col-lg-5">{input}</div><div class="col-lg-3">{error}</div>',
'labelOptions' => ['class' => 'col-sm-2 control-label'], //修改label的样式
]
]);
?>
<?= $form->field($model, 'username')->textInput(['placeholder' => '您的姓名', 'require' => 'true','class' => 'form-control ', 'maxlength' => 255])->label(false) ?>
<?= $form->field($model, 'email')->textInput(['placeholder' => '您的邮箱地址', 'require' => 'true', 'class' => 'form-control', 'maxlength' => 255])->label(false) ?>
<?= $form->field($model,'grade_id')->dropDownList($grades,['prompt'=>'选择年级'])?>
<?= Html::activeDropDownList($model, 's_id', ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>
<div class="form-group"> <?= Html::submitButton('提交', ['class' => 'btn btn-blue-bg-new']) ?> <div class="help-block"></div> </div> <?php ActiveForm::end(); ?>
$model = new Form;
$model->scenario = 'dingyue';
if($model->load(Yii::$app->request->post())){
if(Yii::$app->request->isAjax){
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}else{
if ($model->save()) {
$this->flash('success', '提交成功!');
return $this->redirect(Yii::$app->request->referrer);
} else {
$this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
return $this->refresh();
}
}
}
这样写完后会在系统的session中存一个success或者是error的信息,如果不做下面操作,那么呈现的效果表单提交,页面刷新
这时候用户无法知道自己是否已经提交成功或者失败,需要在页面增加一块代码
<script>
<?php foreach(Yii::$app->session->getAllFlashes() as $key => $message) : ?>
alert('<?=$message?>');
<?php endforeach; ?>
</script>
以上5行代码的意思是:如果后台设置了flash,那么前台页面就把提示的信息alert出来
1 $form = ActiveForm::begin(['action' => ['sykc','id' => $model->id], 2 'method'=>'post', 3 'options' => ['id'=>'w-sykc-form','enctype' => 'multipart/form-data','autocomplete'=>"off",'data-pjax'=>'true'], 4 'fieldConfig' => [ 5 'template' => '{label}<div class="col-lg-5">{input}</div><div class="col-lg-3">{error}</div>', 6 'labelOptions' => ['class' => 'col-sm-2 control-label'], //修改label的样式 7 ] 8 ]);
1 <?=Html::activeHiddenInput($model,'address')?>