CakePHP中两个具有不同值的提交按钮 [英] Two submit buttons with different values in CakePHP

查看:98
本文介绍了CakePHP中两个具有不同值的提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在CakePHP中创建一个表单,该表单中有2个按钮: Accept(值为1)和 Reject(拒绝)0。一个按钮由 $ this->生成; Form-> end()和另一个带有 $ this-> Form-> submit()的表单。提交后,应更新数据库中字段 is_accept 的值,并根据用户选择单击哪个按钮将其更新为0或1。但是我不确定如何设置按钮的值以及如何将值保存到按钮。

I'm trying to create a form in CakePHP, in which there are 2 buttons: 'Accept' with value = 1, and 'Reject' 0. One button is generated with $this->Form->end() and another with $this->Form->submit(). On submission the value of field is_accept in the database should be updated, with 0 or 1 depending on which button the user choose to click. But I'm not sure how to set the values for the buttons and also how to save the value to it.

表单:

echo $this->Form->create('Order');
$options = array(
    'value' => '0',
    'class' => 'btn btn-primary btn-lg pull-right'
);
echo $this->Form->submit('Reject', $options);   

$options = array(
    'label' => __('Accept'),
    'class' => 'btn btn-primary btn-lg', 
    'value' => '1'
);
echo $this->Form->end($options); 


推荐答案

您可以处理按钮的名称,然后使用

You can handle names of the buttons and then use a simple if statement to recognize which button was pressed.

<?php echo $this->Form->create('form_name'); ?>
<?php echo $this->Form->submit('btn_1', array('name' => 'btn')); ?>
<?php echo $this->Form->submit('btn_2', array('name' => 'btn')); ?>

/ *请不要添加与提交按钮相关的任何js,否则两者都会简单地提交而没有任何区别因为$ this-> request-> data ['btn']将不会出现在发布数据中。 * /
Form-> end(); ?>

/* Please don't add any js related with submit buttons else both will simply submit without any difference as $this->request->data['btn'] will not be present in the post data. */ Form->end(); ?>

if($this->request->data['btn'] == 'btn_1') {
 // is btn1 pressed
} else {
 // btn2 pressed
}

这篇关于CakePHP中两个具有不同值的提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆