A form I don’t have any control over is POSTing data to my PHP script. The form contains checkboxes along these lines:
<input type="checkbox" value="val1" name="option"/>
<input type="checkbox" value="val2" name="option"/>
If I were to write the code for the form, I’d write name="option[]" instead of name="option". But this is not a change I can do. Now, if both checkboxes are checked, $_POST["option"] returns just one of the values. How can I, in PHP retrieve all the values selected?
You can read the raw post data. For example:
Check both boxes and the output will be:
Here’s a live demo. All you have to do then is to parse the string yourself, into a suitable format. Here’s an example of a function that does something like that:
Outputs:
There might be a few cases where this function doesn’t work as expected though, so be careful.