提交来自多个文本框的值作为数组 [英] Submit values from multiple textboxes as arrays

查看:118
本文介绍了提交来自多个文本框的值作为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我要我的程序执行的操作是询问用户他们要输入多少个数字.提交值后,程序将使用该值并创建此数量的文本框.每个文本框都会有一个数字,一旦提交,它就应该包含所有数字(不包括初始文本框),并将其存储到数组或其他内容中,以便可以计算均值.

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.

我已经能够创建多达x个文本框,但是找不到提交这些值的方法.

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>

我认为问题在于创建的文本框正在被回显,因此名称为"textbox.$ count".不能用来获取数字?

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!

推荐答案

只需对表单字段名称使用PHP的数组表示法即可:

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

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

会在$_POST['textbox']中生成一个数组,每个以textbox[]名称提交的文本框都会有一个元素.

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

例如:

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

产生

$_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

这篇关于提交来自多个文本框的值作为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆