admin管理员组文章数量:1296843
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?
Here's what I am trying to do:
<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.
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?
Here's what I am trying to do:
<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.
Share Improve this question asked Jun 19, 2014 at 3:28 JasonJason 1,07911 silver badges24 bronze badges1 Answer
Reset to default 10Alternatively, 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>
When you process it:
if(isset($_POST['submit'])){
$all_players = $_POST['player'];
echo '<pre>';
print_r($all_players);
echo '</pre>';
}
It should yield something like this:
Array
(
[0] => Array
(
[username] => Test1
[points] => 1
)
[1] => Array
(
[username] => Test2
[points] => 2
)
)
本文标签: javascriptUsing HTML Fieldsets for PHP form arraysStack Overflow
版权声明:本文标题:javascript - Using HTML Fieldsets for PHP form arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741639990a2389860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论