pufos pufos - 4 months ago 12
PHP Question

php form process - unknown input names for quantity increase / decrease

I have an online store, and the page

mycart.php
will display user`s purchases like this :


image - title - price per unit - quantity - total price


The product IDs and Quantities are held in
$_SESSION
arrays. (Is this good ?)

So if the user want 2 pieces of one product and 3 pieces of another product:


  1. How to generate the
    <input>
    tag (
    <input id="product1" name="product-1647">
    ?)

  2. How to iterate through that input if the attribute
    name="product-1647"
    is dynamic generated?



I've studied a website and they generate inputs like above.

<input type="text" id="product1" name="product-23342">
<inpyt type="text" id="product2" name="product-47385">
...


But i can't figure it out how the post-processing is done.

Any advice will be great.

Answer

html

<input type="text" id="product1" name="product[23342]">
<inpyt type="text" id="product2" name="product[47385]">

php

foreach ($_POST['product'] as $prod_id => $prod_value){
  if ($prod_value){
    // do whatever
  }
}