神奇的POST数组

这两天在研究discuz的后台,发现一些很神奇的东西,稍微百度了一下,发现大家对这个提得比较少。那么我就来说说吧。

我们通常用的post表单一般是这个样子的(我没有关闭表单</form>的习惯,大家还是加上吧……):

<form method="post" action="debug.php">
<input value="hello1" name="hello1">
<input value="hello2" name="hello2">
<input value="提交" type="submit">

嗯,为了方便我们把debug.php代码也贴出来(其实就一句……):

print_r($_POST);

好了,那么我们post一下,得到的是这样的结果:

Array
(
    [hello1] => hello1
    [hello2] => hello2
)

这样的post没有什么好说的,大家都知道。好了,接下来是高级操作了——如果把表单改成这个样子呢:

<form method="post" action="debug.php">
<input value="hello" name="hello[word]">
<input value="hello" name="hello[wolrd]">
<input value="hello" name="hello[test]" type="submit">
</form>

好了,我们来post一下,竟然——

Array
(
    [hello] => Array
        (
            [word] => hello
            [wolrd] => hello
            [test] => hello
        )

)

对啦~在post结果里面我们发现了数组~

那么这个代码可以再发扬一下,大家体会体会吧~比如这样:

<form method="post" action="debug.php">
<input name="foo[]" type="checkbox" value="thisone">
<input name="foo[]" type="checkbox" value="thatone">
<input name="foo[]" type="checkbox" value="another">
<input name="foo[]" type="checkbox" value="and this">
<input name="foo[]" type="checkbox" value="and more">
</form>

这个代码能够返回这样的结果~

Array
(
    [foo] => Array
        (
            [0] => another
            [1] => and this
        )

)

这样只需要遍历一下$_POST['foo']数组就能够得到所有被用户勾选的选项啦~厉害吧~

(用php这么久了也才发现这个玩意我真是失败啊……)

评论

还没有评论。

发表评论

发表评论代表你授权本网站存储并在必要情况下使用你输入的邮箱地址、连接本站服务器使用的 IP 地址和用户代理字符串 (User Agent) 用于发送评论回复邮件,以及将上述信息分享给 Libravatar Akismet,用于显示头像和反垃圾。