对PHP表单数组使用HTML字段集 [英] Using HTML Fieldsets for PHP form arrays
问题描述
我知道您可以将名称数组用于多个表单输入(例如<input type="text" name="username[]">
,但是也可以将其用于整个字段集吗?如何使用PHP $ _POST global来操纵它?
I know you can use name arrays for multiple form inputs (e.g. <input type="text" name="username[]">
, but can this also be used for entire fieldsets? How would this be manipulated with the PHP $_POST global?
这就是我想要做的:
<fieldset name="player[]">
<input type="text" name="username">
<input type="number" name="points">
</fieldset>
<fieldset name="player[]">
<input type="text" name="username">
<input type="number" name="points">
</fieldset>
之所以尝试这样做,是因为我正在构建一个允许用户动态添加/减去玩家"字段集的表单.如果您有比我要求的解决方案更好的解决方案,请随时提供替代方案.
The reason why I am trying to do this is because I am building a form that allows the user to dynamically add/subtract "player" fieldsets. If you have a better solution than what I was asking for, please feel free to provide an alternative.
推荐答案
或者,如果您希望进行这种分组,则可以按照以下形式创建分组:考虑以下示例:
Alternatively, if you want such grouping you could create a grouping in your form such as this: consider this example:
<form method="POST" action="">
<fieldset>
Username: <input type="text" name="player[0][username]" />
Points: <input type="number" name="player[0][points]" />
</fieldset>
<fieldset>
Username: <input type="text" name="player[1][username]" />
Points: <input type="number" name="player[1][points]" />
</fieldset>
<br/>
<input type="submit" name="submit" />
</form>
处理时:
if(isset($_POST['submit'])){
$all_players = $_POST['player'];
echo '<pre>';
print_r($all_players);
echo '</pre>';
}
它应该产生如下内容:
Array
(
[0] => Array
(
[username] => Test1
[points] => 1
)
[1] => Array
(
[username] => Test2
[points] => 2
)
)
这篇关于对PHP表单数组使用HTML字段集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!