Okay, I know that there are a few of these questions already on SO; but they don't actually solve my problem.
For this (cut-down) html form:
<!DOCTYPE html><html><head><title>test</title></head><body>
<form method="post" autocomplete="on" action="application6.php">
<p>
<input type="text" name="fullname" id="fullname" maxlength="50" size="30">
<label for="fullname">*Full Name</label>
</p>
<p>
<input type="text" name="email" id="email" maxlength="50" size="30">
</p>
<p>
<select name="hours" id="hours">
<option value="" disabled="disabled" selected="selected">*Hours</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
</body>
</html>
I have the following code
$error_array = array(
" "
);
echo ("debug: ");
print_r($_POST);
$variables = array(
'fullname',
'email',
'hours'
);
foreach ($variables as $variable_name) {
if (isset($_POST[$variable_name])) {
echo 'Variable: ' . $variable_name . ' is not set<br/>';
array_push($error_array, " " . $variable_name);
}
}
This results, in the submission of a blank form, in the output of
debug: Array ( [fullname] => [email] => )
An alternative is to check if these elements are empty(), but empty() will return false positives for when "hours" is set to 0.
Why are these elements incorrectly registering as being set, and what means can successfully test inputs of different types?
trim
is your friend, or!= ''