dtzjvj3915 2013-03-12 14:19
浏览 184

将多个文本框中的值作为数组提交

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条回答 默认 最新

  • doudi4137 2013-03-12 14:23
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程