开发者

how can I recieve this form in cart.php

$index = 1;
foreach($product['varieties'] as $variety){     
echo '<input style="width:10px; margin-left:9px; " name="price_' . $index . '" type="checkbox" value="' . $variety['price']. '"  />';   
echo '<input name="size_' . $inde开发者_运维问答x . '" type="text" value="' . $variety['size']. '"  />';    $index++; 
} 

if you can see This will have an index=1 and it will be incrementing where each iteration will be price_1, price_2, etc.and size_1,size_2. Now with a dynamic name="" input how can I receive in the cart.php when each name will be different?

it would be something like $price= "'..',$_POST['price_']"? well I have not idea how can I receive this name index from the cart.php url.

Thnank you.


Instead of string building the name like size_1, why not make the name like this size[].

Then you can instantly access it like an array via PHP.


Look at alex's answer. It is better suited to the circumstances..

You could, at the end, add a hidden text box with the number of items (the $index value). and read it back

so after the loop add

echo '<input name="counter" type="hidden" value="' . $index. '"  />'

and when you try to read it, the first thing would be to read the $POST['counter'] and then loop again from 1 to that value reading the $POST['price_'.$loop_counter]


You could find the values with something like this:

$prices = preg_grep('/^price_\d+$/', $_POST);

foreach($prices as $P) {
   $idx = substr($p, 5); // extract the digits, which we know will be at position 5->end

   $size = $_POST['size_' . $idx];
   etc....
}

This assumes for that for every price_#, there's a corresponding size_#

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜