Forms with a variable number of fields
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 开发者_StackOverflow社区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.
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.
Name your inputs like this:
<input type="text" name="myName[]" />
<input type="text" name="myName[]" />
<input type="text" name="myName[]" />
Then, in your PHP, $_POST['myName']
or $_GET['myName']
is an array.
This is documented here.
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.
Sounds fine to me.
To expand on what @Michael has said in his comment, you need to turn the name='id'
into an array, like this (PHP):
echo '<input type="text" name="id[' . $i . ']">'
Where $i
is incremented every loop.
When the form is posted, you can access this array like this:
$_POST['id']['1']
Replacing ['1']
with whatever you want.
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.
精彩评论