如何对表单元素进行分组

2022-08-30 15:05:04

我得到了这个表格:

<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)

现在,我将点数组与组数组进行比较/pare,因此:

点[0]被“连接到”组[0]等?


答案 1

事实证明,您可以使用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号。您只能用作最后一组括号。[]


答案 2

我正在扩展这个答案,因为我花了一段时间来跟踪将解析表单中的数据的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/>";
    }
}

}

希望这个答案有所帮助!


推荐