Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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(); ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
866 views
Welcome To Ask or Share your Answers For Others

1 Answer

<?= 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...