CakePHP 2.0 Determine which submit button has been clicked

后端 未结 4 1748
暖寄归人
暖寄归人 2021-02-06 00:20

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

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


        
4条回答
  •  梦如初夏
    2021-02-06 01:06

    Don't use the same name for both submit buttons. Consider this example:

    Form->create(false); ?>
    Form->text('input'); ?>
    Form->submit('Yes', array('name' => 'submit1')); ?>
    Form->submit('No', array('name' => 'submit2')); ?>
    Form->end(); ?>
    

    debug($this->request->data) will produce the following when the "Yes" button is clicked:

    array(
        'submit1' => 'Yes',
        'input' => 'test'
    )
    

    And here it is when the "No" button is clicked:

    array(
        'submit2' => 'No',
        'input' => 'test'
    )
    

    To check which button was clicked:

    if (isset($this->request->data['submit1'])) {
        // yes button was clicked
    } else if (isset($this->request->data['submit2'])) {
        // no button was clicked
    }
    

提交回复
热议问题