Skip to content Skip to sidebar Skip to footer

POSTing An Arbitrary Number Of Records From An HTML Form

I'm making a web page in which I have a list of products, along with a field next to each product in which a customer is meant to enter the quantity they want to order. The list of

Solution 1:

You can create form fields with array notation, for example:

<input type="text" name="quantity[productid]">

So you could dynamically generate some fields in your form like this:

<input type="text" name="quantity[3]">
<input type="text" name="quantity[4]">
<input type="text" name="quantity[2]">

And then in PHP it will become an array you can loop over easily:

foreach ($_POST['quantity'] as $productId => $quantity) {
    echo (int) $productId . ':' . (int) $quantity;
    //etc.
}

Post a Comment for "POSTing An Arbitrary Number Of Records From An HTML Form"