CakePHP 2.0确定单击了哪个提交按钮

时间:2012-04-16 10:36:03

标签: forms cakephp cakephp-2.0

在CakePHP 1.3中,您可以创建一个包含多个提交按钮的表单:

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

并检测控制器中按下了哪个提交按钮:

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

在CakePHP中,$ this-> params ['form']未设置,点击的按钮值不会出现在$ this-> request,$ this-> request-> ; data,$ this-> params,$ this-> data或$ _POST

如何确定在CakePHP 2.0中点击了哪个按钮?

提前致谢。

编辑:

根据要求,这是表格的代码:

<?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()?>

表格的输出:

<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>

4 个答案:

答案 0 :(得分:19)

通常,对两个提交按钮使用相同的名称是不好的做法。 $ _POST和$ this-&gt;请求 - &gt;数据中应该有一个“提交”键

我在CakePHP 2.1.1中对此进行了测试,如下所示:

观看代码:

<?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()?>

控制器中的$ this-&gt; request-&gt; data:

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

在$ _POST中:

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

您还可以为这两个提交不同的名称:

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

这样您可以在$ _POST或$ this-&gt; request-&gt;数据中区分它们,因为密钥将是提交的名称:

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

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

然后要确定按下哪个按钮,您可以使用简单的isset($ _ POST [''])或$ this-&gt; request-&gt;数据?

答案 1 :(得分:7)

不要对两个提交按钮使用相同的名称。考虑这个例子:

<?php echo $this->Form->create(false); ?>
<?php echo $this->Form->text('input'); ?>
<?php echo $this->Form->submit('Yes', array('name' => 'submit1')); ?>
<?php echo $this->Form->submit('No', array('name' => 'submit2')); ?>
<?php echo $this->Form->end(); ?>
单击“是”按钮时,

debug($ this-&gt; request-&gt; data)将产生以下内容:

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

这是点击“否”按钮的时候:

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

检查点击了哪个按钮:

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

答案 2 :(得分:0)

2.0中的

不再有$this->params['form'] 所有表单助手发布的字段最终都在$this->data中(无论如何都更有意义)

所以

if (!empty($this->data['submit']) && $this->data['submit'] == "Submit 1") {}

请注意!empty()在这里也更好。

PS:您可以使用我的增强型升级shell在代码中替换它:https://github.com/dereuromark/upgrade

它的命令

cake Upgrade.Upgrade request

(https://github.com/dereuromark/upgrade/blob/master/Console/Command/UpgradeShell.php#L833)

答案 3 :(得分:0)

if (!empty($this->request->data['submit']) && $this->request->data['submit'] == "Yes") {
// do your stuff
}