Jump to content

Handling input from a form with lots of input fields


rgrne

Recommended Posts

I have to deal with input that is typed into many, many text fields of an HTML form by users.
The HTML form currently uses a series of statements like:
<input type="text" name="input1">
<input type="text" name="input2">
.
.
.
<input type="text" name="inputN">

Where N is a large number in the multiple hundreds range.

This results in a series of variables ($input1, $input2, $input3.....etc.) whose string values are the text
inputs by the user.

I would like to put all these values in an array (among other things). How do I do this without wrting hundreds of lines that say
$array[1]=$input1;
$array[2]=$input2;
$array[3]=$input3;
etc. etc.

I would like to have a FOR loop that just goes through all the values and puts them into the array.
However, I can't find a way to increment the names of the *variables*, i.e. $input1, $input2, $input3 etc.

Things like using "input".$n, and incrementing $n, don't work.

Nor have I been able to find a way to change the HTML to use statments like input type="text" name="array[10]" and thus use array elements as variable names to begin with.

Any suggestions?
Link to comment
Share on other sites

Use

[code]<input type="text" name="input[]">
<input type="text" name="input[]">
.
.
.
<input type="text" name="input[]">[/code]

When the form is submitted, $_POST['input'] is an array of those values, so

[code]foreach ($_POST['input'] as $value) {

           // process form value, in this example echo it

           echo $value . '<br />';

}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.