GOAL: echo switch statement case which maps/matches to 'postType'
PROBLEM: Regardless of what I try, the $_POST data seems to vanish, thus the switch statements default case is triggered and the form shows again instead of the intended/desired echo statement that I hope to show based on the postType contained within the $filter variable. I am working in PHP 5.4
A var_dump returns: array(2) { ["postType"]=> NULL ["myContent"]=> string(4) "dsfs" }
I believe that I have both the form and the switch statement syntaxs/formatting correct. My problem, i think is somewhere in how the data is or isn't being passed to the switch via the filter_input_array(INPUT_POST, [...])
my humble code stub:
//test data
//$_POST = [ 'postType' => 'myPlayby', 'myContent' => 'Anna King', ];
//build the array of data created by from for switch checks
$filter = filter_input_array(INPUT_POST, [ 'postType' => [], 'myContent' => [], ]);
var_dump($filter);
echo "<br /><br />";
//$search = false;
switch ($filter['postType'])
{
case 'myChar':
echo "Character Check: " . $filter['myCharacter'];
$search = $filter['myCharacter'];
break;
//test data ought to trigger myPlayby case showing 'Playby check: Anna King' here... frack
case 'myPlayby':
echo "Playbe Check: " . $filter['myPlayby'];
$search = $filter['myPlayby'];
break;
case 'myFoobar':
echo "Foo Check: " . $filter['myFooBar'];
$search = $filter['myFooBar'];
break;
default:
echo '<form type="submit" method="post" action="' . THIS_PAGE . '"; >
<select name="postTypes">
<option default disable>-------------</option>
<option value="myChar">Character</option>
<option value="myPlayby">Playby</option>
<option value="myFooBar">Foo</option>
</select>
<input type="text" value="" name="myContent"/>
<input type="submit" name="submit" value="Submit">
</form>' ;
break;
}
This is my first time working with filter_input_array to handle $_POST data, and after a day of trial, error, research and effort which include looking at Lynda.com, searching Google and reading what relevant posts I could find on StackOverflow, php.net, etc, I am flummoxed.
I've found the answer, i was correct in that the issue was in how I set up the filter_input_array.
By refactoring the code like so:
$filter = filter_input_array(INPUT_POST, [
'postType' => [ 'filter' => FILTER_SANITIZE_STRING ],
'contents' => [ 'filter' => FILTER_SANITIZE_STRING ],
]);
$search = false; //in the switch, false is reassigned value of the sanitized string and all is happiness