Yii2.0 模态弹出框+ajax提交表单,yii2.0ajax_PHP教程

WBOY
Release: 2016-07-12 08:50:36
Original
1085 people have browsed it

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

如题 我们使用模态弹出框+ajax提交表单 首先我们把index视图的create按钮添加data-toggle 和 data-target。

代码如下:

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

在index视图添加如下代码 来显示模态弹出框:

<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>

Copy after login

修改后的效果如下

然后我们修改控制器中的create方法,把render改为renderAjax即可

   return $this->renderAjax('create', [
    'model' => $model,
   ]); 
   
Copy after login

如果想添加表单验证我们需要修改views 里的 _form 添加上id

<&#63;php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'],'id'=>'leave-form-self']) &#63;>
指向 你的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]
  ];
 } 

Copy after login

效果如下

这样就实现了ajax提交表单

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133111.htmlTechArticleYii2.0 模态弹出框+ajax提交表单,yii2.0ajax 如题 我们使用模态弹出框+ajax提交表单 首先我们把index视图的create按钮添加data-toggle 和 data-target。...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!