字段数量可变的表单 [英] Forms with a variable number of fields

查看:68
本文介绍了字段数量可变的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一种市场.市场上的每种商品都有一种类型(即所售商品),一个值,一个数量,一个卖家名称和一个ID,所有这些信息都存储在MySQL数据库中.

当用户查看市场时,将返回市场中的所有条目.此外,输入文本字段将出现在每个条目的末尾.用户可以输入任何数字,介于空白(0)和最大可用数量之间.

我的问题是,由于每个输入字段都是未知迭代的while循环的结果,所以我不能简单地将名称硬编码到每个字段中.我尝试使用计数器变量来跟踪和创建名称,但是它似乎没有用.例如:name='.$i++.'

在相关说明中,为了使程序正常运行,每个字段都需要发送两个值-id(我可以使用它来查找条目的其余值)和用户希望购买的数量.我一直在输入数量字段之前使用隐藏字段进行此操作.我不确定这是否是解决此问题的好方法,但我应该注意,就我所知,无需将此ID保密.

一切都清楚了,这是一个基本的UI样机: http://img850.imageshack.us/img850/2654/marketui.jpg

请注意,以开头的列由用户输入的值组成(即100s是用户输入的文本字段的值).麻烦的是,尽管在此示例中有2行,但是实际的数目不是确定的,因此我不能为每个输入字段硬编码名称.

解决方案

对于第一个问题,很简单.做:

for ($i = 0; $i < sizeOf($returnedSQLarray); $i++) {
//your display code
....

....
echo("<input name='quantity-$i'>");
}

尽管我仍然很难想象为什么您不能以某种方式在数量的name属性中使用产品的ID.但是,for()循环对于这种循环非常有用,因此,如果确实需要这种方式,就可以了.

隐藏的输入很好.甚至很标准.任何想要发送的人都可以向您的Web服务器发送他们想要的任何数据,因此无论如何,您只应该真正关心服务器端的安全性.

I'm attempting to create a sort of marketplace. Each item on the market has a type (i.e. the item being sold), a value, a quantity, a seller name, and an id, and all of this information is stored in a MySQL database.

When the user views the market, all entries in the market are returned. In addition, an input text field appears at the end of each entry. The user can enter any number between leaving it blank (0) and the max quantity available.

My problem is that since each input field is the result of a while loop of unknown iterations, I can't simply hard code a name into each field. I've tried using a counter variable to keep track and make names, but it doesn't seem to be working. For example: name='.$i++.'

On a related note, in order for the program to work, each field needs to send two values - the id (which I can use to look up the rest of the entry's values) and the quantity that the user wishes to purchase. I've been doing this with a hidden field before the input quantity field. I'm not sure if this is a good way of going about this, but I should note that there's no need as far as I can tell for the id to be kept secret.

Just so everything's clear, here's a basic UI mockup: http://img850.imageshack.us/img850/2654/marketui.jpg

Note that the column starting with consists of user inputted values (i.e. the 100s are the values of user-inputted text fields). The hitch is that while there are 2 rows in this example, the actual number is not deterministic, and so I can't hard code names for each input field.

解决方案

For the first problem, simple. Do:

for ($i = 0; $i < sizeOf($returnedSQLarray); $i++) {
//your display code
....

....
echo("<input name='quantity-$i'>");
}

although I still am having trouble imagining why you can't use the product's ID in the quantity's name attribute somehow. for() loops are great for this kind of looping, though, so if this really is the way you need to handle it you're covered.

The hidden input is fine. Pretty standard, even. Anyone who wants to can send your webserver whatever data they want, so you should only really care about serverside security anyway.

这篇关于字段数量可变的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆