tags:

views:

48

answers:

3
$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_' . $index . '" 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.

+2  A: 

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.

alex
A: 
Gaby
I would prefer my method, instead of needing to keep track of a limit. What if someone (malicious) sends a higher number? More to check / debug / can go wrong.
alex
true .. i am coming from a non PHP background.. Your solution is more elegant ..
Gaby
That's OK - PHP has tonnes of little tricks to make web dev easier. This is one of the more useful ones.
alex
If I understood is to take the counter off and use like everyone usually does it.foreach($product['varieties'] as $variety){ echo '<input style="width:10px; margin-left:9px; " name="price[]" type="checkbox" value="' . $variety['price']. '" />'; echo '<input name="size[]" type="hidden" value="' . $variety['size']. '" />'; } what about putting the type=hidden where only the scripter will be able to see it.
jona
@Jona, As @alex mentioned, a user can alter the html even if it is hidden (so a malicious one can play tricks with the script.)..
Gaby
@jona Putting it as hidden will take me about 2 seconds to circumvent.
alex
A: 

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_#

Marc B
You have an uppercase and lowercase p there.
alex