Menus

Tuesday, 29 November 2016

Multiple submit button in Yii2 view forms


Posting multiple submit button value to controller

check a specific submit button value in controller (Yii2)


Solution

First create view form with multiple submit button.


<?= Html::submitButton('Submit 1', ['name' => 'submit', 'value' => 'submit_1']) ?>
<?= Html::submitButton('Submit 2', ['name' => 'submit', 'value' => 'submit_2']) ?>

Controller action

If (\Yii::$app->request->isPost) {
   switch (\Yii::$app->request->post('submit')) {
      case 'submit_1':

      case 'submit_2':

   }
}
When you submit form by pressing enter (without click any submit button), submit_1 will be default value.

or other methord

if (Yii::$app->request->post()) {

  if (Yii::$app->request->post('submit') == 'submit_1') {
     //Code for submit button 1
  }

  if (Yii::$app->request->post('submit') == 'submit_2') {
    //Code for submit button 2
  }

}

or other methord

public function actionYourControllerName()
{
    if(isset($_POST['submit') && $_POST['submit']=='next') 
    {
      // your code
    } 
    else if(isset($_POST['submit']) && $_POST['submit']=='previous') 
    {
       // your code
    }
}





No comments:

Post a Comment