I have a loop below that is showing a quantity box and includes a hidden field containing the product name.
I want these to be tied together so if out of 100 inputs the
Rob, I see you have a good answer but you might like to be aware of a significant issue.
By posting independent quantities[]
and products[]
, then you are relying on the two serializations being conformal with each other - ie. that both are serialized in DOM order - hence that the indexes of $_POST['quantity']
and $_POST['product']
correspond element-by-element. For me, this is not a completely safe assumption - see the selected answer here.
It would be far safer, and more conventional, to have one field per product, named with a representation of product-id and a value representing quantity. Thus, product-ids and their values are guaranteed to correspond.
Client-side and server-side code would need to be reviewed accordingly.