For some reason I cannot set the loop time because I am generating the form dynamically using jQuery. I have done some research for the topic,normally will use foreach
to loop for all valid fields but I'm not sure how to do these:
<form action="testing.php" method="post" >
<input type="text" name="product[1][name]" value="product1"/>
<input type="text" name="product[1][color][]" value="product1color1"/>
<input type="text" name="product[1][color][]" value="product1color2"/>
<input type="text" name="product[1][color][]" value="product1color3"/>
<input type="text" name="product[2][name]" value="product2"/>
<input type="text" name="product[2][color][]" value="product2color1"/>
<input type="text" name="product[3][name]" value="product3"/>
<input type="text" name="product[3][color][]" value="product3color1"/>
<input type="text" name="product[4][name]" value="product4"/>
<input type="text" name="product[4][color][]" value="product4color1"/>
<input type="submit" />
And my testing code ended up like these, it is not working .=(
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$product=$_POST['product'];
//store everything that start with product into array
foreach($product as $key){
//loop for product.1 product.2 and so on.....
//echo name of current product
echo $product[$key]['name'];
foreach($product[$key]['color'][] as $point){
echo $point;
}//loop for every single available color field
}//end of product loop
}// end of post request
?>