CakePHP 2.0确定已单击哪个提交按钮 [英] CakePHP 2.0 Determine which submit button has been clicked
问题描述
在CakePHP 1.3中,您可以使用多个提交按钮创建表单:
echo $ this-> Form->提交('Submit 1',array('name'=>'submit');
echo $ this-> Form-&提交');
并检测控制器中按下哪个提交按钮:
if(isset($ this-> params ['form'] ['submit'])&& $ this-& ['form'] ['submit'] ==Submit 1){
//第一个按钮点击
}
在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 / addid =HistoryAddFormmethod =postaccept-charset =utf-8>
< div style =display:none;>
< input name =_ methodvalue =POSTtype =hidden>
< / div>
< div class =submit>
< input name =submitvalue =Yestype =submit>
< input name =submitvalue =Notype =submit>
< / div>
< / form>
解决方案一般来说,为两个提交按钮。
在$ _POST和$ this-> request-> data
中应该有一个submit键我在CakePHP 2.1.1中测试了这个
视图代码:
<?php echo $ this-> Form-> create('Message',array('action'=>'test'));
//额外的测试输入字段
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-> request-> data中的控制器中:
array(
'submit'=>'Yes',
'Message'=& test'=>'TestFieldTest'
)
)
_POST:
数组(
'_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-> request->数据中区分它们,因为它们是不同的:
键将是提交者的名字:数组(
'submitY'=>'是',
'Message'=> array(
'test'=>'foo'
)
)
数组'=>'POST',
'data'=> array(
'Message'=> array(
'test'=>'Bar'
)
),
'submitY'=>'是'
)
然后,要确定按下哪个按钮,您可以使用简单的isset($ _ POST [''])或$ this-> request-> data?
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>
解决方案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 ?
这篇关于CakePHP 2.0确定已单击哪个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!