Question

I have a form as below which allows users to add more input field through a addmore button . my question is how can i retrieve the array in POST and echo them out . as you can see i have two input fields with name product[] and quantity[] (more fields can be added) . i have to get the value of the field using a a foreach loop and store it in a varibla $message to be used in mail(). i have tried a for each loop for both and my for each loop code as below

$product = $_POST['product'];
$quantity = $_POST['quantity'];

foreach($product as $value)
{
    $message = "<html><head>
        <title></title>
        </head>
        <body>
        <table> 
            <tr>
                <td>".$value."";
}

foreach($quantity as $value)
{
    $message.=" </td>
                <td>".$value."</td>
            </tr>
        </table>
        </body>
        </html>";
}

My input form

 <form id="quick_post" method="post"> 
<table id="input_fields">
    <tr> 
        <td><input class="orderinput" type="text" name="product[]" /></td> 
        <td><input class="orderquan" type="text" name="quantity[]" size="1" maxlength="3" /></td>
    </tr>
 </tr>
</table>

<input class="more" type="button" value="Addmore" name="addmore" /> // a jquery script is processed on click to add more fields.       
<input class="send" type="submit" value="Send" name="send" /></form>

the output for this is i get only the first row of products printed and for the remaining row only the quantity is printed or echoed. as below

Product Name  Quantity
aaaaaa       22 
             33 
Was it helpful?

Solution

The problem is that in your first foreach loop you are just overwriting the $message variable and the last product name is passed to the second loop.

Try this

echo "<html><head><title></title></head> <body> <table>";
for($i=0 ; $i < count($product) ; $i++) 
{
    echo "<tr><td>".$product[$i]."</td>";
    echo "<td>".$quantity[$i]."</td></tr>";
}

echo "</table></body></html>";

OTHER TIPS

Not sure but in the first foreach you may have to need to concate the $message as you have done it for the second foreach of $quantity .

$product = $_POST['product'];

$quantity = $_POST['quantity'];

for($i=0 ; $i < sizeof($product) ; $i++) {

        $message.="<tr>
            <td>".$product[$i]."</td>
            <td>".$quantity[$i]."</td>
              </tr>";

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top