CakePHP 2.0 Determine which submit button has been clicked

RichardAtHome picture RichardAtHome · Apr 16, 2012 · Viewed 15.9k times · Source

In CakePHP 1.3 you can create a form with multiple submit buttons:

echo $this->Form->submit('Submit 1', array('name'=>'submit');
echo $this->Form->submit('Submit 2', array('name'=>'submit');

and detect which submit button was pressed in the controller with:

if (isset($this->params['form']['submit']) && $this->params['form']['submit'] == "Submit 1") {
  // first button clicked
}

In CakePHP, $this->params['form'] isn't set and the clicked button value doesn't appear anywhere in $this->request, $this->request->data, $this->params, $this->data or $_POST.

How do I determine which button has been clicked in CakePHP 2.0?

Thanks in advance.

Edit:

As requested, here's the code for the form:

<?php echo $this->Form->create('History', array('action'=>'add')); ?>
<div class='submit'>
<?php 
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submit')); 
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submit')); 
?>
</div>
<?php echo $this->Form->end()?>

And the output of the form:

<form action="/projects/kings_recruit/trunk/www/histories/add" id="HistoryAddForm" method="post" accept-charset="utf-8">
  <div style="display:none;">
    <input name="_method" value="POST" type="hidden">
  </div>
  <div class="submit">
    <input name="submit" value="Yes" type="submit">
    <input name="submit" value="No" type="submit">
  </div>
</form>

Answer

Borislav Sabev picture Borislav Sabev · May 4, 2012

Generally it is a bad practise to use the same name for both submit buttons. There should be a "submit" key in the $_POST and $this->request->data

I tested this in CakePHP 2.1.1 as shown below:

The view code:

<?php echo $this->Form->create('Message', array('action'=>'test')); 
//  Extra test input field
echo $this->Form->input('test');
?>

<div class='submit'>
<?php 
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submit')); 
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submit')); 
?>
</div>
<?php echo $this->Form->end()?>

The in the controller in $this->request->data:

array(
    'submit' => 'Yes',
    'Message' => array(
        'test' => 'TestFieldTest'
    )
)

And in $_POST:

array(
    '_method' => 'POST',
    'data' => array(
        'Message' => array(
            'test' => 'TestFieldTest'
        )
    ),
    'submit' => 'Yes'
)

You can also give the two submits different names:

echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submitY')); 
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submitN')); 

This way you can differ them in the $_POST or $this->request->data, because the keys will be the submits' names:

array(
    'submitY' => 'Yes',
    'Message' => array(
        'test' => 'foo'
    )
)

array(
    '_method' => 'POST',
    'data' => array(
        'Message' => array(
            'test' => 'Bar'
        )
    ),
    'submitY' => 'Yes'
)

Then to determine which button is pressed you can use a simple isset($_POST['']) or over $this->request->data ?