从HTML表单过帐任意数量的记录 [英] POSTing an arbitrary number of records from an HTML form
问题描述
我正在制作一个网页,其中有一个产品列表,每个产品旁边都有一个字段,客户可以在其中输入他们要订购的数量.产品列表是从数据库生成的,因此产品数量未知.有没有一种方法可以发布所订购的每种产品的数量以及每种产品的ID(来自数据库)?
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 products is generated from a database and therefore the number of products isn't known. Is there a way of POSTing the quantity of each product ordered along with the IDs (from the database) of each product?
谢谢
本
推荐答案
您可以使用数组符号创建表单字段,例如:
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]">
然后在PHP中它将变成一个可以轻松循环的数组:
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.
}
这篇关于从HTML表单过帐任意数量的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!