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!

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Do you mean to put: <input type="text" name="textbox[]" /> 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! –
echo '<input type="text" etc...';... not being mean or anything, but you should learn basic PHP syntax.
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?
you'd put the array access code into the means.php file, because that's where the form's submitting to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.