Tie two inputs together (text & hidden)?

后端 未结 3 769
天命终不由人
天命终不由人 2021-01-25 07:04

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

3条回答
  •  灰色年华
    2021-01-25 07:50

    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.

提交回复
热议问题