Jump to content

How to properly pass $_POST data to switch case statement using using filter_input_array


Chezshire

Recommended Posts

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: error_reporting(E_ALL | E_STRICT);

define('THIS_PAGE',basename($_SERVER['PHP_SELF']));


//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.test-switch-stack.php

My friend JD helped slipped me a solution that works nicely.

 

slight refactoring

$filter = filter_input_array(INPUT_POST, [
'postType' => [ 'filter' => FILTER_SANITIZE_STRING ],
'contents' => [ 'filter' => FILTER_SANITIZE_STRING ],
]);

//var_dump($filter);
//echo "<br /><br />";

$search = false; //in the switch, false is reassigned value of the sanitized string and all is happiness

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.