I'm getting: 3 warnings (Illegal offset type in isset or empty in...) 3 notice (Undefined index: ...)
The 3 warnings are realated with the first foreach. And the 3 notices are related with the second foreach loop.
I don't really get what I'm doing wrong... Need some help out here.
<form name="form" action="index.php" method="POST">
<input type="text" name="name" value="<?php if (isset($_POST['name'])) { echo $_POST['name']; } ?>" />
<input type="text" name="age" value="<?php if (isset($_POST['age'])) { echo $_POST['age']; } ?>" />
<input type="text" name="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" />
<input type="submit" />
</form>
<?php
$expected = array(
'name' => array("filter" => FILTER_SANITIZE_STRING),
'age' => array("filter" => FILTER_SANITIZE_NUMBER_INT),
'email' => array("filter" => FILTER_SANITIZE_EMAIL)
);
foreach ($expected AS $key => $value) {
if (!isset($_POST[$value])) {
echo "not set";
} elseif (empty($_POST[$value])) {
echo "empty";
}
}
$result = filter_input_array(INPUT_POST, $expected);
foreach ($result AS $key => $value) {
if (!$result[$value]) {
echo "not valid value";
}
}
?>
Thank you all.