【问题标题】:How can I group form elements如何对表单元素进行分组
【发布时间】:2011-08-13 17:01:39
【问题描述】:

我收到了这个表格:

<form method="post" action="" accept-charset="utf-8"> 
<p>
  <label>first_field</label><br />
  <input type="text" id="first_field" name="points[]" /><br />
  <input type="radio" value="inside" name="group_1" checked /><br />
  <input type="radio" value="outside" name="group_1"><br />
</p>
<p>
  <label>second_field</label><br />
  <input type="text" id="second_field" name="points[]" /><br />
  <input type="radio" value="inside" name="group_2" checked /><br />
  <input type="radio" value="outside" name="group_2"><br />
</p>
</form>

我想要完成的是检查是否检查了内部或外部,如果外部我检查了给定文本输入的乘以 1,5 点。顺便说一句,这需要在 PHP 中计算。

我该怎么做?

更新

Array
(
[bonus] => Array
    (
        [points] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 0
                [5] => 0
                [6] => 0
                [7] => 0
                [8] => 0
                [9] => 0
                [10] => 0
                [11] => 0
                [12] => 0
                [13] => 0
                [14] => 0
            )

        [group] => Array
            (
                [0] => inside
                [1] => outside
                [2] => outside
                [3] => inside
                [4] => inside
                [5] => inside
                [6] => inside
                [7] => inside
                [8] => outside
                [9] => inside
                [10] => inside
                [11] => inside
                [12] => outside
                [13] => inside
                [14] => inside
            )

    )

)

上面是 print_r($_POST) 的结果

现在我如何比较/比较点数组和组数组,所以:

points[0] 被“连接”到 group[0] 等?

【问题讨论】:

  • 要获得更多语义标记,请使用
    标签而不是那些

    标签,并使用 而不是

  • 绝对不要使用标签来标记字段组。标签与单个字段相关联(该字段需要是标签的子字段,或者使用 for 属性引用)

标签: php html forms


【解决方案1】:

您只需要在$_POST 变量中捕获它返回的内容,然后对其进行处理。如果您在填写表格后发送var_dump($_POST),您应该对该怎么做有更好的了解。

【讨论】:

  • 或带有
     标签的 print_r($_post) :)
【解决方案2】:

事实证明,您可以使用 HTML 表单对字段进行分组。在此处查看此代码:(特别注意name 属性)

<form method="post" action="" accept-charset="utf-8">
<p>
  <label>first_field</label><br />
  <input type="text" id="first_field" name="field[1][points]" /><br />
  <input type="radio" value="inside" name="field[1][group]" checked /><br />
  <input type="radio" value="outside" name="field[1][group]"><br />
</p>
<p>
  <label>second_field</label><br />
  <input type="text" id="second_field" name="field[2][points]" /><br />
  <input type="radio" value="inside" name="field[2][group]" checked /><br />
  <input type="radio" value="outside" name="field[2][group]"><br />
</p>
</form>

不填写任何内容,这将产生一个像这样的 POST 数组:

Array
(
    [field] => Array
        (
            [1] => Array
                (
                    [points] => 
                    [group] => inside
                )

            [2] => Array
                (
                    [points] => 
                    [group] => inside
                )

        )
)

我希望这回答了您的问题,这是一个巧妙的小技巧,我还没有真正看到很多其他人讨论过。需要注意的一点是,您需要在任何一组括号中手动指定一个 ID 号。您只能使用[] 作为最后一组括号。

【讨论】:

  • 谢谢多米尼克,这正是我要找的。​​span>
【解决方案3】:

我正在扩展this answer,因为我花了一段时间才找到解析表单数据的 PHP 代码。

在 HTML 中使用这种技术将产生一个键值对数组。

  <input type="text" id="first_field" name="field[1][points]" /><br />
  <input type="radio" value="inside" name="field[1][group]" checked /><br />
  <input type="radio" value="outside" name="field[1][group]"><br />

这就是我使用 PHP 解析数组的方式。

foreach ($_POST as $record => $detail) {
// The submit button from my HTML form was POSTing data
// so I used an if statement to remove it from the result set
if(empty($firstRow))
{
    $firstRow = 1;
}
else
{
    // since $detail is still an array, you have to loop through it again
    foreach ($detail as $key => $value) {
            echo $key."<br/>";
            // $key would contain the key value (1 or 2)
            echo $value['points']."<br/>";
            echo $value['group']."<br/><br/>";
    }
}

}

希望这个答案有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2012-03-12
    • 2013-01-23
    相关资源
    最近更新 更多