PHP developers decided that values posted from HTML select tag with attribute multiple, somehow overwrites some internal variable data. Basically it goes like this – you have the following form with a select tag:
<form action="/post.php" method="post">
<select name="MyMultiSelect" multiple>
<option>one</option>
<option>two</option>
<option>three</option>
<option>four</option>
</select>
<input type="submit" value="send">
</form>
and in PHP you try to access $_POST (An associative array of variables passed to the current script via the HTTP POST method):
var_dump($_POST);
And guess what? When you select, lets say one, two and three in browser, the array looks something like:
array(1) { ["MyMultiSelect"]=> string(5) "three" }
Strangely, the cause is some overwrite that is described in PHP FAQ: How do I get all the results from a select multiple HTML tag? For me it is mystery, why did PHP developers choose to overwrite values posted by HTTP standard post?
The problem is, that they propose to add square brackets to HTML code, like:
<select name="MyMultiSelect[]" multiple>
Beside, that I do not like ‘[]’ in my Valid HTML 4.01 Strict code, the problem is, that you may not have access to that HTML code. In my case it is legacy application, that is available in binary form only.
The solution that I come up is, to use raw $_POST data using PHP file_get_contents in combination with PHP input/output streams namely php://input
php://input allows you to read raw POST data.
Here is a sample code:
$data_from_post = file_get_contents("php://input");
var_dump($data_from_post);
And output is:
string(...) "MyMultiSelect=one&MyMultiSelect=two&MyMultiSelect=three"