Jump to content

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


Chezshire
Go to solution Solved by 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

Link to comment
Share on other sites

  • Solution

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.