Determine which button triggered the Controller Action

I have a form in which there are 2 submit buttons

echo Html::submitButton("<span class='glyphicon glyphicon-floppy-disk' aria-hidden='true'></span> ".Yii::t('app', 'Save'), ['class' => 'btn btn-success', 'title'=>'Save without closing', 'id'=>'btn-save', 'value' => '0']);

echo Html::submitButton("<span class='glyphicon glyphicon-floppy-disk' aria-hidden='true'></span> ".Yii::t('app', 'Close'), ['class' => 'btn btn-primary', 'title'=>'Save and close','id'=>'btn-close',, 'value' => '1']);

In my controller action, how can I distinguish which button triggered the post?

if ($model->load(Yii::$app->request->post())) {
   // if(  *???????????*   ){
   //     btn-close triggered the action
   // }else{
   //     btn-save triggered the action
   // }
}

Thank you for the help!

I’ve tried

(Yii::$app->request->post('submit') === '0' ? 'save' : 'close');

but it always returns close

I should probably note that I use

$('form#{$model->formName()}').on('beforeSubmit', function(e){ 
$.post(
     \$form.attr("action"), //serialize Yii2 form 
     \$form.serialize()
)

in my form, is this the issue? If so, what workaround could I employ?

The workaround I’ve managed to get functional is to use a hidden input in which my btn click sets a value and I use that value in the controller as I don’t seem to be able to directly access the btn’s value directly.

Hi.
My solution is made by define two form submit buttons with additional ‘name’ and ‘value’ html attributes.

<?= Html::submitButton($book->isNewRecord ? 'Zapisz i zakończ' : 'Popraw ',
          [
            'class' => $book->isNewRecord ? 'btn btn-success' : 'btn btn-primary',
              'name' => 'subaction',
              'value'=> 'SaveBook',
          ]
        ); 
      ?>
      <?= Html::submitButton('Dopisz wybranego autora',
          [
            'class' => 'btn btn-primary',
              'name' => 'subaction',
              'value'=> 'LinkNextAuthor',
          ]
        ); 
      ?>

Then in controler you can simply check the ‘subaction’ post variable value:

$request = Yii::$app->request;
//...
if($request->post('subaction') == 'SaveBook')
{...}
if($request->post('subaction') == 'LinkNewAuthor')
{...}

I tried giving both buttons the same name and a value yet in the controller

Yii::$app->request->post('NameOfButton')

never returned a value?

I will try once more because it would be a much cleaner approach than my workaround!

Nope, tried and it always returns null and I don’t understand why. I thought it might be because I set different id’s to the buttons, so I set them to the same as the name so everything would be identical, but it made no difference. My gut is thinking it has to do with the Javascript processing of the form and form.serialize, but this is just a thought and haven’t had time to dig further (already put a lot of time).

Yes, serialize does seem to be my issue https://stackoverflow.com/a/4007951

It could be that serialize JS code is what causes the problem. In my example there is no any additional form processing, just simple \yiibootstrap\ActiveForm instance which produces simple submit buttons html:

<button type="submit" class="btn btn-success" name="subaction" value="SaveBook">Zapisz i zakończ</button>
<button type="submit" class="btn btn-primary" name="subaction" value="LinkNextAuthor">Dopisz wybranego autora</button>

Then, the request POST data send to the controller are:

array (size=3)
  '_csrf' => string 'NfqJsUcNzy1knX8Y7FpE_JBCqB1y8MlUU4QsyhStOyRTstD3FT2DSCHvJk6nNh2M4AfaajmBrB8850-MWe5_ew==' (length=88)
  //the form data
  'Books' => 
    array (size=3)
      'name' => string 'test title' (length=10)
      'genre_id' => string '14' (length=2)
      'newAuthorId' => string '92' (length=2)
  //the clicked button's value
  'subaction' => string 'LinkNextAuthor' (length=14)

Hope it could help a bit… when you make the time for it :slight_smile:
Regards.