Yii2.0 modal pop-up +ajax submit form _php Instance

Source: Internet
Author: User

The title we use the modal pop-up +ajax submit the form first we add data-toggle and Data-target to the Create button in the index view.

The code is as follows:

<?php 
Echo html::a (' Add leave form ', [' 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 control of the Create method, the render changed to Renderajax can be

   return $this->renderajax (' Create ', [
    ' model ' => $model,
   ]); 
   

If you want to add form validation we need to modify the _form in the views to add the ID

<?php $form = Activeform::begin ([' Options ' => [' enctype ' => ' multipart/form-data '], ' id ' => ' Leave-form-self ']?>
point to your models validation rule public

 function rules () {return [[
   ' t_leave] in the rules add _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 ' => ', [[
   ' T_ IMG ', ' reviewer_user ', ' audit_user '], ' string ', ' Max ' => 255]
  ;
 } 

The effect is as follows

This enables Ajax to submit the form

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.