1

Basically what I want my program to do is to ask the user how many numbers they want to enter. Once the value is submitted, the program will take the value and create this amount of textboxes. Each textbox will take a number and once submitted, it should take all the numbers (excluding the initial text box) and store it into an array or something so the mean can be calculated.

I've been able to get as far as creating x amount of textboxes but cannot find a way to submit these values.

<html>
<body>

<form action="means.php" method = "get">
Enter sample size: <input type = "number" name = "size" <br>
<input type = "submit">

<?php 
if ( isset($_GET["size"] ) )
{
$size = $_GET["size"];
$count = 1;
while ($count <= $size)
    {
    echo '<br><input type=\"text\"  name=\"textbox".$count."\" />';
    $count++;

    }
}
?>
</form>



</html>
</body>

I assume the problem is that the textboxes created are being echoed so the name "textbox.$count." cannot be used to obtain the numbers?

Any help would be greatly appreciated. Thanks in advance!

Virus.exe
  • 11
  • 1
  • 1
  • 2

1 Answers1

4

Just use PHP's array notation for form field names:

<input type="text" name="textbox[]" />
                                ^^---force array mode

which will produce an array in $_POST['textbox'], one element for each textbox which was submitted with the textbox[] name.

e.g:

<input type="text" name="textbox[]" value="1" />
<input type="text" name="textbox[]" value="foo" />
<input type="text" name="textbox[]" value="banana" />

produces

$_POST = array(
   'textbox' => array )
         0 => 1,
         1 => 'foo',
         2 = > 'banana'
   )
)

Your problem is that you're using single-quoted strings ('), meaning that

$var = 'foo';
echo '$var'  // outputs $, v, a, r
echo "$var"  // outputs f, o ,o
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Do you mean to put: where the echo is, in my code? Using that without echo and single quotes, it gives an error so I'm unsure of what to do. Thanks for you help! – – Virus.exe Mar 12 '13 at 14:38
  • But say I want to get the values that are submitted in a different php file, how would I obtain the values produced by the array? – Virus.exe Mar 12 '13 at 14:49
  • you'd put the array access code into the `means.php` file, because that's where the form's submitting to. – Marc B Mar 12 '13 at 14:58