Home >Backend Development >PHP Tutorial >Yii2.0 modal pop-up box + ajax submission form

Yii2.0 modal pop-up box + ajax submission form

高洛峰
高洛峰Original
2017-01-09 11:08:111635browse

As the title says, we use modal pop-up box + ajax to submit the form. First, we add data-toggle and data-target to the create button of the index view.

The code is as follows:

<?php
echo Html::a(&#39;添加请假单&#39;, [&#39;create&#39;], [&#39;class&#39; => &#39;btn btn-success&#39;,&#39;data-toggle&#39;=>&#39;modal&#39;,&#39;data-target&#39;=>&#39;#ajax&#39;])
?>

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

Yii2.0 模态弹出框+ajax提交表单

Then we modify the create method in the controller and change render to renderAjax

return $this->renderAjax(&#39;create&#39;, [
 &#39;model&#39; => $model,
]);

If we want to add form validation, we need to modify _form in views and add the id

<?php $form = ActiveForm::begin([&#39;options&#39; => [&#39;enctype&#39; => &#39;multipart/form-data&#39;],&#39;id&#39;=>&#39;leave-form-self&#39;]) ?>
指向 你的models 在rules添加上验证规则
 
 public function rules()
 {
  return [
   [[&#39;t_leave_date&#39;, &#39;t_days&#39;, &#39;t_reason&#39;, &#39;t_nickname&#39;,&#39;t_leave_enddate&#39;], &#39;required&#39;],
   [[&#39;t_leave_date&#39;, &#39;t_leave_enddate&#39;], &#39;safe&#39;],
   [[&#39;t_days&#39;], &#39;number&#39;],
   [[&#39;t_reason&#39;], &#39;string&#39;],
   [[&#39;type&#39;, &#39;add_time&#39;, &#39;uid&#39;, &#39;update_time&#39;, &#39;status&#39;, &#39;is_shen&#39;], &#39;integer&#39;],
   [[&#39;t_pickup&#39;, &#39;t_nickname&#39;, &#39;t_pass&#39;], &#39;string&#39;, &#39;max&#39; => 20],
   [[&#39;t_img&#39;, &#39;reviewer_user&#39;, &#39;audit_user&#39;], &#39;string&#39;, &#39;max&#39; => 255]
  ];
 }

The effect is as follows

Yii2.0 模态弹出框+ajax提交表单

In this way, the ajax submission form is realized

For more Yii2.0 modal pop-up box + ajax submission form related articles, please Follow PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn