蛋糕php中的多个复选框

时间:2012-12-15 03:15:52

标签: function cakephp checkbox

我有一个预订列表,我想使用多个复选框删除它们。这是我的代码:

foreach ($bookings as $booking): ?>
    <tr>
        <td><?php echo h($booking['Booking']['first_name']); ?>&nbsp;</td>
        <td><?php echo h($booking['Booking']['surname']); ?>&nbsp;</td>
        <td><?php echo h($booking['Booking']['created']); ?>&nbsp;</td>
        <td><?php echo $this->Form-> checkbox('Bookings.ID.['.$booking['Booking']['ID'].']', 
        array('value' => $booking['Booking']['ID']));?></td>    
    </tr>
<?php endforeach; ?>

在我的控制器中我使用此功能删除所选的预订:

public function deletebooking(){

    $bookings = $this->Booking->find('all');
    $this->set('bookings',$bookings);
    if(!empty($this->data)){
        foreach($this->data[Bookings] as $key => $value){

            if($value != 0){
                $this->Booking->delete($value);
                $this->redirect(array('action' => 'index'));
            }

        }
    }

}

有谁能告诉我它为什么不起作用?

1 个答案:

答案 0 :(得分:1)

将输入字段更改为以下内容: foreach($ bookings as $ booking):?&gt;

<td>
   <?php echo $this->Form->checkbox('bookingids', 
                                      array(
                                        'value' => $booking['Booking']['ID'],
                                        'name' => 'data[Booking][bookingids][]',
                                       ));?>
</td> 

注意名称值中的空[]。这将创建该数组的新索引。在您的控制器中,您可以像这样访问它:

   if(!empty($this->data)) :
        foreach($this->data['Bookings']['bookingids'] as $key => $value):
                $data = array();
                $data['Booking']['id'] = $value;
                $this->Booking->delete($data);
        endforeach;
        $this->redirect(array('action' => 'index'));
    endif;