问题描述
**在Cakephp中只有1个动作的相同表单上的2个按钮的解决方案是什么? **
** what is solution for 2 buttons on same form with only 1 action in cakephp? **
i have following code,1 form contain 2 buttons print & ship,if i click
print button,page is redirected on printorder page but problem is , if i click ship
button it is not working & page is redirected on printorder.
==========================================================================
<?php // in index.ctp file
echo $this->Form->create('User',array('action'=>'orders','type' =>'post'));
echo $this->Form->submit('Ship',array('name'=>'user','value'=>'Ship','id'=>'1'));
echo $this->Form->submit('Print',array('name'=>'user','value'=>'Print','id'=>'2'));
echo $this->Form->end();
?>
<?php // in UserController
public function orders()
{
if($this->params->data['form']['user'] = "Print" )
{
$this->redirect(array('action'=>'printorder'));
}
if($this->params->data['form']['user'] = "Ship")
{
$this->redirect(array('action'=>'shiporder'));
}
}
?>
推荐答案
我按照以下方式投票(未选中的解决方案) ,建议对每个提交按钮应用不同的值,然后检查提交的内容。
I followed the top voted (not selected solution) from Two submit buttons in one form, which suggested applying different values to each submit button, and checking those on submit.
但是,尽管设置了'value'键入
$ this-> Form-> submit
的 $ options
数组中的code在生成的元素上设置。因此,我遵循了,并使用名称
值键。
However, CakePHP didn't easily play with this technique, as despite setting
'value'
key in the $options
array of $this->Form->submit
, no value was set on the generated element. So, I followed the suggestions from here, and used the 'name'
value key.
view.ctp
$this->Form->submit('Submit 1', array('name' => 'action1'));
$this->Form->submit('Submit 2', array('name' => 'anotherAction'));
controller.php
if (array_key_exists('action1', $this->request->data)) {
/** setup any data you want to retain after redirect
* in Session, Model, or add to redirect URL below
*/
// redirect to another action
$this->redirect(array('action' => 'myOtherAction'));
} else if (array_key_exists('anotherAction', $this->request->data)) {
// do something with anotherAction submit
}
这篇关于在CakePHP中单击了哪个提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!