我有一个类似这样的表单:
<form action="video-sort.php" method="get">
<h3>Game</h3>
<ul>
<li><input id="game-180man" type="checkbox" name="game" value="180man">180man</li>
<li><input id="game-18man" type="checkbox" name="game" value="18man">18man</li>
<li><input id="game-mtt" type="checkbox" name="game" value="MTT">MTT</li>
</ul>
<h3>Type</h3>
<ul>
<li><input id="type-lecture" type="checkbox" name="type" value="Lecture">Lecture</li>
<li><input id="type-liveplay" type="checkbox" name="type" value="Liveplay">Liveplay</li>
<li><input id="type-tutorial" type="checkbox" name="type" value="Tutorial">Tutorial</li>
</ul>
<input type="submit" />
</form>
如果检查了每组中的第一个,我会得到URL:
video-sort.php?game=180man&type=Lecture
太棒了!但是,当每个类别中有多个复选框被选中时,我会得到(例如,如果我全部选中):
video-sort.php?game=180man&game=18man&game=MTT&type=Lecture&type=Liveplay&type=Tutorial
不行!它只需要最后的论证。
此外,我尝试了一种name=game[]
和name=type[]
的形式,但URL最终只是在名称中添加括号(例如:video-sort.php?game[]=180man&game[]=18man
,而不是将它们放在一起。
我想得到的网址阅读:
video-sort.php?game=180man,18man,MTT&type=Lecture,Liveplay,Tutorial
我怎样才能做到这一点?如有任何帮助,我们将不胜感激!
编辑:这是一个Wordpress网站。我正在创建一个后过滤器,据我所知,为了让Worpress理解查询,参数需要在一个逗号分隔的列表中。如果我使用括号,我会得到错误:
Warning: preg_split() expects parameter 2 to be string, array given in /wp-includes/query.php on line 1694
(该文件的来源:http://phpxref.com/xref/wordpress/wp-includes/query.php.source.html)
没错,
?game[]=180man&game[]=18man
只是向数组添加键。保持原样。
真正正确的方法是使用数组语法(game[]),这样在您的video-sort.php代码中,您可以像这样获取整个数组:
$games = $_GET['game'];
foreach ($games as $game) {
// do something
}
如果你真的必须让游戏参数是一个逗号分隔的列表,你可能会想使用javascript(即jquery)来获取一个检查值的列表,创建列表,然后将其附加到你的表单帖子中。工作量太大,容易出错。如果你的一个游戏的值中有逗号怎么办?