How to post an array for checkboxes from html

In most of the cases, we just need simple values to be posted to server. For example,
<input type="checkbox" value="test1" name="checkboxtest"></input>

After we check it and submit, we will receive the following structure:
'checkboxtest' => 'test1'

But the problem is, if we add another checkbox with the same name:
<input type="checkbox" value="test1" name="checkboxtest"></input>
<input type="checkbox" value="test2" name="checkboxtest"></input>

We can only receive 'checkboxtest' => 'test2'. 'test1' will be lost.
How to save both test1 and test2 into an array? We can do it like this:
<input type="checkbox" value="test1" name="checkboxtest[]"></input>
<input type="checkbox" value="test2" name="checkboxtest[]"></input>

The symbol '[]' will tell browser to put both test1 and test2 into an array. So the post data will be:
'checkboxtest' => array ( 'test1', 'test2')

评论

此博客中的热门博文

Openwrt路由器上配置shadowsocks透明代理+gfwlist(PAC)

Configure shadowsocks transparent proxy + gfwlist(PAC) on OpenWRT Router

Using Haproxy + shadowsocks (ha + ss) to setup multi ss backend and load balance