4

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(); ?>
3
  • can you post your form code? Commented Jul 21, 2016 at 6:41
  • show your view code .. Commented Jul 21, 2016 at 6:41
  • <?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(); ?> Commented Jul 21, 2016 at 6:49

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. it seems working. But, I have to click the button two times.The first click does not do anything. Is there anything more to do?
Your problem is same to github.com/yiisoft/yii2/issues/10555. When you click submit button, if that button has attribute name, yii.activeForm.js when add a hidden input with name is same to button's name. See github.com/yiisoft/yii2/pull/10625/files for the solution
Do not name the button submit, otherwise you’ll have to click twice on the button to submit the form.
Note: This only works with normal form submission and not when using Pjax for form submission
@VănQuyết I have tried it but still the same result. I have to click the submit button twice
1

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.

4 Comments

As per the above comments, do not name it 'submit'. otherwise you will need to click twice because it uses jquery to add a hidden variable
@LionelYeo I have named both of them separately but still it hits the 0 condition
@Faisal try name=>"action" val=>"0" and name="action" , val=>"1"
@LionelYeo again same issue
0

Try This :

View File

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'status')->checkbox(); ?>
<div class="form-group">
  <?= Html::submitButton('NEXT',[ 'name'=>'submit', 'value' => 'next', 'class' => 'btn btn-primary pull-right']) ?>
  <?= Html::submitButton('PREVIOUS',[ 'name'=>'submit', 'value' => 'previous', 'class' => 'btn btn-primary pull-right') ?>
</div>  
<?php ActiveForm::end(); ?>

Controller File

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

1 Comment

is there any other way rather than to use isset($_POST['submit')) >?
0

Also you can add this little js snippet to your project and bind it to beforeSubmit event in yii.activeForm.js like this:

(function ($) { 
    var formId = !!yiiconfig.viewPolicyParams && yiiconfig.viewPolicyParams.formId && yiiconfig.viewPolicyParams.formId, 
        $form = formId && $("#" + formId);

    /**
     * Updates hidden field that represents clicked submit button.
     * @param event event object triggered
     */
    function updateHiddenButton (event) {
        var $buttons = $form.find(':submit');

        $buttons.length && $buttons.each(function (i,b) {
            var $hiddenButton = $('input[type="hidden"][name="' + $(b).attr('name') + '"]', $form);
            $hiddenButton.length && $hiddenButton.remove();
        });

    };

    $form && $form.bind('beforeSubmit.' + formId, updateHiddenButton);

} (jQuery));

This code removes all hidden inputs which are being created by yii.activeForm before submitting. Then after this inputs will be recreated by yii.activeForm.

hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.