Reputation: 25
so I have a issue that I cannot shake, I am confident I am doing something simple wrong, but none the less I cannot find the mistake, I have a html select element with 2 option elements, the select is set to multiple and both options are set for selected, all you have to do is click the Apply button and see what the result is.
So my issue is this, when you run this scenario and do a variable dump on filter input, it returns false(no data), but when you variable dump the $_POST global it has the data I am looking for, any idea what i am doing wrong.
here is the code:
$submit = filter_input(INPUT_POST, 'submit', FILTER_SANITIZE_SPECIAL_CHARS);
if(!isset($submit)){
?>
<form action="" method="post">
<select name="partsused[]" id="usedparts" multiple>
<option value="empty" selected>Empty</option>
<option value="full" selected>Full</option>
</select>
<br/>
<button type="submit" name="submit" value="submit">Apply</button>
</form>
<?php
} else {
$someArray = filter_input(INPUT_POST, 'partsused');
var_dump($someArray);
var_dump($_POST['partsused']);
die;
}
The output I get from the var_dump is as follows :
bool(false)
array(2) { [0]=> string(5) "empty" [1]=> string(4) "full" }
So as you can see the filter_input does not read the value at all from the $_POST as it should, I have tried all the different filters even not filtering at all.
so just to make this clear, yes assigning the S_POST['partsused'] to the variable $someArray does then work, but I dont like doing that, I always use filter_input, not sure why this is doing this, any ideas or takes.
Upvotes: 2
Views: 890
Reputation: 24276
You must use filter_input_array instead
$someArray = filter_input_array(INPUT_POST, 'partsused');
Upvotes: 0
Reputation: 14550
As per this comment, if the input is an array and you are using filter_input
, you need to use the following setup:
var_dump(filter_input(INPUT_POST, 'partsused', FILTER_DEFAULT , FILTER_REQUIRE_ARRAY));
and as per the documentation:
FILTER_REQUIRE_ARRAY
- Requires the value to be an array.
Upvotes: 1