我只从表单中获取一个字段的参数
I get just params of one field from 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
)
...
)
您的所有输入都具有相同的名称,因此只有最后一个会通过。
<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>
我会这样做,将项目组合在一起,而不是像其他答案建议的那样使用三个数组
你需要像这样把输入变成一个数组
<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=""
属性已更改为在它们之后有 []
。
在所有输入中添加数组,如
<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>
你会得到你想要的....
我在某种形式上有一个 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
)
...
)
您的所有输入都具有相同的名称,因此只有最后一个会通过。
<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>
我会这样做,将项目组合在一起,而不是像其他答案建议的那样使用三个数组
你需要像这样把输入变成一个数组
<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=""
属性已更改为在它们之后有 []
。
在所有输入中添加数组,如
<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>
你会得到你想要的....