【问题标题】:Cakephp Pass array of checkboxes to another controllerCakephp 将复选框数组传递给另一个控制器
【发布时间】:2013-03-20 16:40:15
【问题描述】:

我想做的是为用户提供一种从库存中结帐多种产品的方法。

我的产品索引页面(列出所有要签出的可用产品)如下所示:

<?php echo $this->Form->create('multi');?>
<?php foreach ($products as $product): ?>
<tr class="hovertable">
    //All the fields go here
    <td style="cursor: default">
        <?php echo $this->Html->link($this->Html->image('tr/Checkouts_Add.png') . " " . __('Checkout'), array('controller' => 'Checkouts','action' => 'add', $product['Product']['id']), array('escape' => false, 'class' => 'button')); ?>
        <?php echo $this->Html->link($this->Html->image('tr/Edit.png'), array('action' => 'edit', $product['Product']['id']), array('escape' => false)); ?>
        <?php echo $this->Form->postLink($this->Html->image('tr/Delete.png'), array('action' => 'delete', $product['Product']['id']), array('escape' => false), __('Are you sure you want to delete # %s?', $product['Product']['id'])); ?>
        <?php echo $this->Form->input('Product.id.'.$product['Product']['id'] ,
            array('label' => false,
                  'type' => 'checkbox',
                  'id'=>'listing_'.$product['Product']['id'])); ?>
    </td>
</tr>
<?php endforeach; ?>
<?php echo $this->Form->submit(__('Submit'));?>

然后在我的结帐控制器中,我添加了一个新功能来结帐多个项目,我希望这个表单由检查的产品填充

    public function multi($count = 1) {
    if($this->request->is('post')) {            
        foreach($this->request->data['Checkout'] as $data) {
            //Do not forget this line. you need to create new model for saving each time.
            if ($this->request->isPost()) {
                $this->Checkout->create();
                $this->Checkout->save($data);
            } else {
                $this->request->data['Checkout']['product_id'] = $productId;
            }
        }
        $this->redirect(array('action' => 'index'));
    }
    $products = $this->Checkout->Product->find('list');
    $users = $this->Checkout->User->find('list');
    $this->set(compact('products', 'users'));
    $this->set('count', $count);
}

如您所见,我尝试添加我认为可能可行的帽子,但产品索引页面中的提交按钮没有任何作用。任何帮助将不胜感激!

【问题讨论】:

    标签: php arrays cakephp cakephp-2.0


    【解决方案1】:

    我只是检查了几次,至少我知道你失败的原因之一是你的 foreach 你有一些 errorwarning 因为我不知道你的产品数组的价值我检查这段代码对我来说效果很好:

    <?php $products=array('0'=>'11','1'=>'22','2'=>'333');
    echo $this->Form->create('User');?>
    <?php foreach ($products as $product): ?>
    <tr class="hovertable">
        //All the fields go here
        <td style="cursor: default">
    
            <?php echo $this->Form->input('Product.id.'.$product,
                array('label' => false,
                      'type' => 'checkbox',
                      'id'=>'listing_'.$product)); ?>
        </td>
    </tr>
     <?php
     endforeach;
    
    echo $this->Form->end(__('Submit')); 
    

    如果你没有错误或警告,我建议你不要检查

    $this->Form->postLink

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      • 2016-01-31
      相关资源
      最近更新 更多