Home > Article > Backend Development > Yii2.0 modal pop-up box ajax submission form, yii2.0ajax_PHP tutorial
As the title says, we use modal pop-up box ajax to submit the form. First, we add data- to the create button of the index view. toggle and data-target.
The code is as follows:
<?php echo Html::a('添加请假单', ['create'], ['class' => 'btn btn-success','data-toggle'=>'modal','data-target'=>'#ajax']) ?>
Add the following code in the index view to display the modal pop-up box:
<div class="modal bs-example-modal-lg" id="ajax"> <div class="modal-dialog"> <div class="modal-content width_reset" id="tmpl-modal-output-render"> </div> </div> </div>
The modified effect is as follows
Then we modify the create method in the controller and change render to renderAjax
return $this->renderAjax('create', [ 'model' => $model, ]);
If you want to add form validation, we need to modify _form in views and add the id
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'],'id'=>'leave-form-self']) ?> 指向 你的models 在rules添加上验证规则 public function rules() { return [ [['t_leave_date', 't_days', 't_reason', 't_nickname','t_leave_enddate'], 'required'], [['t_leave_date', 't_leave_enddate'], 'safe'], [['t_days'], 'number'], [['t_reason'], 'string'], [['type', 'add_time', 'uid', 'update_time', 'status', 'is_shen'], 'integer'], [['t_pickup', 't_nickname', 't_pass'], 'string', 'max' => 20], [['t_img', 'reviewer_user', 'audit_user'], 'string', 'max' => 255] ]; }
The effect is as follows
This implements the ajax submission form