Jump to content


Photo

Handling input from a form with lots of input fields


  • Please log in to reply
1 reply to this topic

#1 rgrne

rgrne
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 13 May 2006 - 06:38 PM

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?


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 13 May 2006 - 06:47 PM

Use

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

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

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

           // process form value, in this example echo it

           echo $value . '<br />';

}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users