POSTing an arbitrary number of records from an HTML form

I am creating a web page in which I have a list of products, as well as a box next to each product in which the customer must indicate the quantity that they want to order. The list of products is generated from the database, and therefore the number of products is unknown. Is there a way to POSTING the quantity of the ordered product along with the identifiers (from the database) of each product?

thanks

Ben

+3
source share
2 answers

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

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

So, you can dynamically generate some fields in your form:

<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 that you can easily iterate over:

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

, , , .

, javascript , .

0

Source: https://habr.com/ru/post/1706457/


All Articles