目标:echo switch语句案例,用于映射/匹配' postType'
问题:无论我尝试什么,$ _POST数据似乎都消失了,因此触发了switch语句的默认情况,并且表单再次显示而不是我希望根据包含的postType显示的预期/期望的echo语句在$ filter变量中。我在PHP 5.4中工作
var_dump返回: array(2){[" postType"] => NULL [" myContent"] => string(4)" dsfs" }
我相信我的表单和switch语句语法/格式都正确。我的问题,我认为在某些方面,数据是如何通过filter_input_array(INPUT_POST,[...])传递给交换机的
我简陋的代码存根:
//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;
}
这是我第一次使用filter_input_array处理$ _POST数据,经过一天的试用,错误,研究和努力,包括查看Lynda.com,搜索Google并阅读我在StackOverflow上可以找到的相关帖子, php.net等,我很沮丧。
答案 0 :(得分:0)
我找到了答案,我说的是问题在于我如何设置filter_input_array。
通过重构代码:
$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