我只从形式得到一个字段的参数

时间:2017-07-19 10:39:00

标签: php zend-framework zend-form

我在某个表单上有一个foreach,当我提交时,我会得到控制器中最后一个字段的所有参数。

形式:

<form action="<?php echo Mage::getUrl('checkout/cart/addmultiple')?>" method="post" id="products_addtocart_form" enctype="multipart/form-data">
    <?php foreach ($_productCollection as $prod) : ?>
        <div class="border-new-cart-button">
             <input type="hidden" name="productId" value="<?php echo $productId ?>" />
             <input type="hidden" name="productName" value="<?php echo $prod->getName() ?>" />
             <input type="text" class="input-qty-product number-control" title="Qty" value="<?php echo /*$this->getProductDefaultQty() * */ 0 ?>" name="qty" id="qty-<?php echo $productId ?>"/>
        </div>
    <?php endforeach; ?>
    <button class="btn add-to-cart" onclick="this.form.submit()"><span><?php echo $this->__('Validate') ?></span></button>
</form>

控制器:

public function addmultipleAction(){
    $params= array($this->getRequest()->getParams());
    var_dump($params);
    // Result:
      Array
           (
             [0] => Array
                         (
                           [productId] => 106
                           [productName] => shirt
                           [qty] => 6
                           [productTagName] =>
                         )

            )
}

修改

Now i get them like this:

[productId] => Array
                    (
                        [0] => 106
                        [1] => 107
                        [2] => 108
                        [3] => 109
                    )

[qty] => Array
                (
                    [0] => 4
                    [1] => 3
                    [2] => 2
                    [3] => 1
                )


// I want to group the `id` and `qty` per product, something like this:
 array(
      [0] (
           [id] => 106
           [qty] => 4
          )

      [1] (
           [id] => 107
           [qty] => 3
          )
       ...
 )

3 个答案:

答案 0 :(得分:2)

您需要将输入输入到像这样的数组

RenderTransform

注意:<input type="hidden" name="productId[]" value="<?php echo $productId ?>" /> <input type="hidden" name="productName[]" value="<?php echo $prod->getName() ?>" /> <input type="text" name="qty[]" class="input-qty-product number-control" title="Qty" value="<?php echo /*$this->getProductDefaultQty() * */ 0 ?>" id="qty-<?php echo $productId ?>"/> 属性已更改为name=""之后。

答案 1 :(得分:0)

您的所有输入都具有相同的名称,因此只会传递最后一个。

<form action="<?php echo Mage::getUrl('checkout/cart/addmultiple')?>" method="post" id="products_addtocart_form" enctype="multipart/form-data">
<?php for ($i=0, $length = count($_productCollection); $i < $length; $i++) : ?>
    <div class="border-new-cart-button">
         <input type="hidden" name="item[<?=$i?>][productId]" value="<?php echo $productId ?>" />
         <input type="hidden" name="item[<?=$i?>][productName]" value="<?php echo $_productCollection[$i]->getName() ?>" />
         <input type="text" class="input-qty-product number-control" title="Qty" value="<?php echo /*$this->getProductDefaultQty() * */ 0 ?>" name="item[<?=$i?>][qty]" id="qty-<?php echo $productId ?>"/>
    </div>
<?php endfor; ?>
<button class="btn add-to-cart" onclick="this.form.submit()"><span><?php echo $this->__('Validate') ?></span></button>

我会这样做,将项目组合在一起,而不是像其他答案那样有三个数组

答案 2 :(得分:0)

在所有输入中添加数组,例如

<form action="<?php echo Mage::getUrl('checkout/cart/addmultiple')?>" method="post" id="products_addtocart_form" enctype="multipart/form-data">
<?php foreach ($_productCollection as $prod) : ?>
    <div class="border-new-cart-button">
         <input type="hidden" name="productId[]" value="<?php echo $productId ?>" />
         <input type="hidden" name="productName[]" value="<?php echo $prod->getName() ?>" />
         <input type="text" class="input-qty-product number-control" title="Qty" value="<?php echo /*$this->getProductDefaultQty() * */ 0 ?>" name="qty[]" id="qty-<?php echo $productId ?>"/>
    </div>
<?php endforeach; ?>
<button class="btn add-to-cart" onclick="this.form.submit()"><span><?php echo $this->__('Validate') ?></span></button>

你会得到你想要的......