Cakephp生成的表单不提交所有数据

时间:2013-09-21 09:06:42

标签: php forms cakephp

我有一个名为Prices.ctp的视图,其中包含以下代码。它创建了一个表单,用于回显来自名为$ products的变量的所有数据,该变量包含名为Products的表中的所有数据。

<?php echo $this->Form->create('Product', array('action' => 'changePrice')); ?>
<fieldset>
    <h3>Products</h3>
        <?php
            foreach($products as $k=>$v){
                echo $this->Form->hidden('id', array('value'=> $v["Product"]['id']));
                echo $this->Form->input('name', array('value' => $v["Product"]["name"] ));
                echo $this->Form->hidden('slug', array('value'=>$v["Product"]['slug']));
                echo $this->Form->hidden('description', array('value'=>$v["Product"]['description']));
                echo $this->Form->hidden('cateID', array('value'=>$v["Product"]['cateID']));
                echo $this->Form->input('price', array('value' => $v["Product"]['price']));
                echo $this->Form->hidden('photo', array('value'=>$v["Product"]['photo']));
                echo $this->Form->hidden('photo_dir', array('value'=>$v["Product"]['photo_dir']));
                echo $this->Form->hidden('active', array('value'=>$v["Product"]['active']));
                echo $this->Form->hidden('views', array('value'=>$v["Product"]['views']));
                echo $this->Form->hidden('created', array('value'=>$v["Product"]['created']));
                echo $this->Form->hidden('modified', array('value'=>$v["Product"]['modified']));
    }?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>

它射向这个控制器方法,名为changePrice

public function changePrice(){
        $this->Product->saveMany($this->request->data['Product']);

        $this->Session->setFlash( "Prices Saved.");
        $this->redirect ( "/admin/products/" );
        return;

    }

但是当我使用debug()来检查$ this-&gt; request-&gt;数据的内容时,它表明只发送了视图中foreach循环的最后一次迭代。

如果原始的$ products变量(传递到视图price.ctp)有4个产品:product1,product2,product3和product4,那么所有产品都有自己的数据来自产品表(id,name,等),当在页面上按下提交按钮时,只有product4的变量将被传递到$ this-&gt; request-&gt; data。

为什么会这样?

干杯

1 个答案:

答案 0 :(得分:0)

您可以这样做以获取所有产品的数据

<?php
    foreach($products as $k=>$v){
        echo $this->Form->hidden("Product.{$k}.id", array('value'=> $v["Product"]['id']));
        echo $this->Form->input("Product.{$k}.name", array('value' => $v["Product"]["name"] ));
        echo $this->Form->hidden("Product.{$k}.slug", array('value'=>$v["Product"]['slug']));
        echo $this->Form->hidden("Product.{$k}.description", array('value'=>$v["Product"]['description']));
        echo $this->Form->hidden("Product.{$k}.cateID", array('value'=>$v["Product"]['cateID']));
        echo $this->Form->input("Product.{$k}.price", array('value' => $v["Product"]['price']));
        echo $this->Form->hidden("Product.{$k}.photo", array('value'=>$v["Product"]['photo']));
        echo $this->Form->hidden("Product.{$k}.photo_dir", array('value'=>$v["Product"]['photo_dir']));
        echo $this->Form->hidden("Product.{$k}.active", array('value'=>$v["Product"]['active']));
        echo $this->Form->hidden("Product.{$k}.views", array('value'=>$v["Product"]['views']));
        echo $this->Form->hidden("Product.{$k}.created", array('value'=>$v["Product"]['created']));
        echo $this->Form->hidden("Product.{$k}.modified", array('value'=>$v["Product"]['modified']));
    }
?>
相关问题