Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forms with a variable number of fields

Tags:

php

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.

like image 653
Chris Avatar asked Jan 20 '23 06:01

Chris


2 Answers

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.

like image 84
Lightness Races in Orbit Avatar answered Jan 21 '23 20:01

Lightness Races in Orbit


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.

like image 27
Bojangles Avatar answered Jan 21 '23 21:01

Bojangles