Store textbox values in php array
10,934
Just create a name grouping attribute, so that you'll get an array of inputs instead of just one:
<input type='text' name='Names[]'>
// ^ this is important
Sidenote:
I don't know if this is a typo, but this should be $names
instead of $number
:
$names = $_POST['Names'];
foreach($names as $num){
echo $num . '<br/>';
}
Author by
Matt Scott Gibbs
Updated on June 04, 2022Comments
-
Matt Scott Gibbs almost 2 years
So basically I have the user enter a number on my first screen. Here is test1.php which generates the number of text boxes that the user had previously entered. That number is $input
echo "<form action='test2.php' method='post'>"; for($i=1; $i<=$input; $i++) { echo "Entry $i"; echo "<input type='text' name='Names'>"; } echo "<input type='submit' class='button' name='submit' value='submit'>"; echo "</form>";
Then my test2.php should print all the values entered, but it only prints out the last value entered from test1.php. For example is $input is 4, only the text entered in the 4th text box prints, which is understandable as I don't know how to print all values.
$names=$_POST['Names']; foreach($number as $num){ echo $num; }
Is the problem with the name I gave to the textboxes, or something else? Any help is much appreciated.
-
Admin about 9 yearsUsing an integer as id/name is not a good idea, at least prepend some string
-
Kevin about 9 years@MattScottGibbs sure no prob, glad this helped