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')
<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')
评论
发表评论