php 如何对表单元素进行分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5802057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I group form elements
提问by andkjaer
I got this form:
我得到了这个表格:
<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>
What i want to accomplish is to check if inside or outside is checked, if outside i checked the multiply points for the given text input by 1,5. BTW this needs to be calculated in PHP.
我想要完成的是检查内部或外部是否被检查,如果在外部,我将给定文本输入的乘数检查为 1,5。顺便说一句,这需要在 PHP 中计算。
How can I do that?
我怎样才能做到这一点?
UPDATE
更新
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
)
)
)
Above is the result of print_r($_POST)
以上是print_r($_POST)的结果
Now ho do I compare/pare The points array with the Group array so:
现在我如何将点数组与 Group 数组进行比较/删除,以便:
points[0] gets "connected" to group[0] etc.?
点 [0] 获得“连接”到组 [0] 等?
回答by Dominic Barnes
As it turns out, you can group fields using HTML forms. Check out this code here: (specifically note the name
attributes)
事实证明,您可以使用 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>
Without filling anything in, this will yield a POST array like this:
不填写任何内容,这将产生一个像这样的 POST 数组:
Array
(
[field] => Array
(
[1] => Array
(
[points] =>
[group] => inside
)
[2] => Array
(
[points] =>
[group] => inside
)
)
)
I hope this answered your question, it's a neat little trick I haven't really seen many others discuss. One thing to note is that you'll need to manually specify an ID number in that any set of brackets. You can only use []
as the last set of brackets.
我希望这能回答你的问题,这是一个巧妙的小技巧,我还没有真正看到很多其他人讨论过。需要注意的一件事是,您需要在任何一组括号中手动指定 ID 号。您只能[]
用作最后一组括号。
回答by Brent Connor
I'm expanding on this answerbecause it took me a while to track down the PHP code that would parse the data from the form.
我正在扩展这个答案,因为我花了一段时间来追踪解析表单数据的 PHP 代码。
Using this technique in HTML will result in a key-value pair array.
在 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 />
This is how I used PHP to parse the array.
这就是我使用 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/>";
}
}
}
Hope this answer helps!
希望这个回答有帮助!
回答by Mild Fuzz
You just need to catch what it coming back in the $_POST
variable, and process it. If you do a var_dump($_POST)
after completing your form, you should have a better idea of what to do.
您只需要捕获它在$_POST
变量中返回的内容,并对其进行处理。如果您var_dump($_POST)
在完成表格后执行 a ,您应该更清楚该怎么做。