取消选中cakephp中的值

时间:2014-02-18 08:28:42

标签: php cakephp checkbox

我有以下复选框

<td><?php echo  $this->Form->checkbox('purchase_owner_occupied', array('hiddenField' => false));?>Purchase owner occupied </td>
<td><?php echo  $this->Form->checkbox('purchase_an_investment_property', array('hiddenField' => false));?>Purchase an investment property </td>
<td><?php echo  $this->Form->checkbox('purchase_land', array('hiddenField' => false));?>Purchase Land </td>

当我检查时,那么post值如下所示

[purchase_owner_occupied] => 1 
[purchase_an_investment_property] => 1 
[purchase_land] => 1 

假设我需要编辑这些信息以取消选中purchase_land,当我取消选中purchage_land并检查剩余2时,帖子值将如下所示

[purchase_owner_occupied] => 1 
[purchase_an_investment_property] => 1 

我需要使用extrastep代码检查所有这些值

if ($purchase_owner_occupied==1)  { // }

是否有办法使未经检查的字段具有0值,如下所示?

[purchase_owner_occupied] => 1 
[purchase_an_investment_property] => 1 
[purchase_land] => 0

1 个答案:

答案 0 :(得分:0)

您需要使用 0/1 的不同值,并且还需要hiddenField参数:

<td><?php echo  $this->Form->checkbox('purchase_owner_occupied', array('value' => 'y', 'hiddenField' => 'n')); ?>Purchase owner occupied</td>
<td><?php echo  $this->Form->checkbox('purchase_an_investment_property', array('value' => 'y', 'hiddenField' => 'n')); ?>Purchase an investment property</td>
<td><?php echo  $this->Form->checkbox('purchase_land', array('value' => 'y', 'hiddenField' => 'n')); ?>Purchase Land</td>

如果我只选择第二个复选框,则输出示例:

array(
        'purchase_owner_occupied' => 'n',
        'purchase_an_investment_property' => 'y',
        'purchase_land' => 'n'
    )