简体   繁体   中英

Cakephp generated form not submitting all data

I have a view called Prices.ctp with the following code. It creates a form which echo's all the data from a variable called $products which contains all the data from a table called 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')); ?>

It shoots to this controller method, called changePrice

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

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

    }

However when I used debug() to check the contents of of $this->request->data it shows that only the final iteration of the foreach loop in the view is being sent.

To re-word, if the original $products variable (passed into the view prices.ctp) has 4 products: product1, product2, product3, and product4, all with their own data from the Product table (id, name, etc), when the submit button is pressed on the page, only product4's variables will be passed into $this->request->data.

Why is this happening?

Cheers

you can do like this to get the data of all products

<?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']));
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM