Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check a specific submit button value in controller (Yii2)

how to check a submit button value in controller (Yii2). I am working with multiple submit button.

I tried simple php code. but it is not working.

if(isset($_POST['next']) && $_POST['next']=='gotocartfive') 

code in view is :

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'status')->checkbox(); ?>

</div>

<div class="form-group">
    <?php echo Html::submitButton('NEXT',array('value'=>'gotocartfive','name' => 'next','id'=>'next_summary','class'=>'btn btn-primary pull-right')); ?>
    <?php echo Html::submitButton('PREVIOUS',array('value'=>'previous_four','name' => 'cartfour','class'=>'btn btn-primary pull-left')); ?>
</div>  
<?php ActiveForm::end(); ?>
like image 444
kritika555 Avatar asked Feb 07 '23 12:02

kritika555


2 Answers

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

PHP

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

      case 'submit_2':

   }
}

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

like image 126
Văn Quyết Avatar answered Feb 23 '23 04:02

Văn Quyết


You can try following code.

Code in view file.

<?= Html::submitButton(Yii::t('app', '<i class="fa fa-times"></i>&nbsp;Remove'), ['class' => 'btn red', 'name' => 'submit', 'value' => '0']) ?>
<?= Html::submitButton(Yii::t('app', '<i class="fa fa-check"></i>&nbsp;Save'), ['class' => 'btn blue', 'name' => 'submit', 'value' => '1']) ?>

Code in controller action

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

  if (Yii::$app->request->post('submit') == 0) {
     //Code for value 0
  }

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

}

Please let me know if you've any questions.

like image 25
Mohit Tanwani Avatar answered Feb 23 '23 04:02

Mohit Tanwani