我希望能解决棒球队的问题! ;)
我有一个包含多个文本框的表单 - 每个文本框都有一系列与之关联的复选框。要获得更好的感觉,请使用以下内容创建文件,然后在浏览器中打开。一张图片说千言万语。
<html>
<head></head>
<body>
<h2>Enter Player Name and Positions They Can Play:</h2>
<form name="player" method="POST">
<p>Player 01: <input type="text" name="p1" value="" />
| P <input type="checkbox" name="pitch1" value="P" />
| C <input type="checkbox" name="catch1" value="C" />
| 1B <input type="checkbox" name="first1" value="1B" />
| 2B <input type="checkbox" name="second1" value="2B" />
| SS <input type="checkbox" name="short1" value="SS" />
| 3B <input type="checkbox" name="third1" value="3B" /> |</p>
<p>Player 02: <input type="text" name="p2" value="" />
| P <input type="checkbox" name="pitch2" value="P" />
| C <input type="checkbox" name="catch2" value="C" />
| 1B <input type="checkbox" name="first2" value="1B" />
| 2B <input type="checkbox" name="second2" value="2B" />
| SS <input type="checkbox" name="short2" value="SS" />
| 3B <input type="checkbox" name="third2" value="3B" /> |</p>
<p>Player 03: <input type="text" name="p3" value="" />
| P <input type="checkbox" name="pitch3" value="P" />
| C <input type="checkbox" name="catch3" value="C" />
| 1B <input type="checkbox" name="first3" value="1B" />
| 2B <input type="checkbox" name="second3" value="2B" />
| SS <input type="checkbox" name="short3" value="SS" />
| 3B <input type="checkbox" name="third3" value="3B" /> |</p>
<p><input type="submit" value="Submit" name="Submit" /></p>
</form>
</body>
</html>
因此,如果用户键入:
“Fred”并检查了1B,SS,3B(一垒和游击手)
“Ted”并检查P,2B(投手和二垒)
“乔”并检查了P,3B(投手和三垒)
在提交时,我想看到以下数组(名为roster,从文本框条目命名的子数组和子数组内容只有选中的值:
名册(
弗雷德(1B,SS,3B), 特德(P,2B), 乔(P,3B))
我有信心我可以处理除此之外的所有事情...我只是想了解如何创建每个子数组并将它们与文本框相关联并将复选框值添加到每个数组。 / p>
非常感谢任何帮助!谢谢!
答案 0 :(得分:2)
这不完全是你提出的问题,但管理这种形式的更好方法是选择这样的vars名称:
<p>Player 01: <input type="text" name="player[1][name]" value="" />
| P <input type="checkbox" name="player[1][pitch]" value="P" />
| C <input type="checkbox" name="player[1][catch]" value="C" />
| 1B <input type="checkbox" name="player[1][first]" value="1B" />
| 2B <input type="checkbox" name="player[1][second]" value="2B" />
| SS <input type="checkbox" name="player[1][short]" value="SS" />
| 3B <input type="checkbox" name="player[1][third]" value="3B" /> |</p>
<p>Player 02: <input type="text" name="player[2][name]" value="" />
| P <input type="checkbox" name="player[2][pitch]" value="P" />
| C <input type="checkbox" name="player[2][catch]" value="C" />
...
你会得到类似的东西:
Array
(
[player] => Array
(
[1] => Array
(
[name] => Marcel
[pitch] => P
[short] => SS
[third] => 3B
)
[2] => Array
(
[name] => Auguste
[catch] => C
[first] => 1B
)
[3] => Array
(
[name] => Ulysse
[catch] => C
[second] => 2B
)
)
)
然后,您可以轻松访问每个玩家的每个元素:
$players = $_POST['player'];
echo $players[1]['name'] . '<br/>';
或者你可以构造这种数组:
foreach($players as $key=>$player) {
$temp[] = $player['name'];
unset($player['name']);
$temp[] = $player;
$result[] = $temp;
unset($temp);
}
print_r($result);
答案 1 :(得分:0)
您可以尝试这样做,但最好使用推荐的Casimir et Hippolyte
。
if( isset($_POST['Submit']) )
{
$players = $_POST['players'];
$items = array('pitch', 'catch', 'first', 'second', 'short', 'third');
$roaster = array();
$i=1;
foreach($players as $player)
{
if(!$player) continue;
$roaster[$player]=array();
foreach($items as $item)
{
if( isset( $_POST[$item.$i] ) ) $roaster[$player][] = $_POST[$item.$i];
}
$i++;
}
$roaster = array_filter( $roaster );
}
只需在form
中进行一些更改,如下所示
Player 01: <input type="text" name="players[]" value="" />
...
Player 02: <input type="text" name="players[]" value="" />
...
Player 03: <input type="text" name="players[]" value="" />
...