0

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?

11
  • trim is your friend, or != ''
    – cmorrissey
    Jun 30, 2015 at 19:07
  • trim wont unset the variable, you should use empty() instead, which checks for null, false, 0, and '' - these are always set, ie always present in the $_POST array ( unlike checkboxes ) they are just empty - I see your concern about empty you can also simply check for '' if you like as in != '', if you do look for empty string then trim is your friend : ) Jun 30, 2015 at 19:09
  • Try array_key_exists
    – user2594803
    Jun 30, 2015 at 19:10
  • @cmorrissey already tried trim() - will try (!='') !
    – Stumbler
    Jun 30, 2015 at 19:11
  • @ArtisiticPhoenix I would use empty, but as mentioned, there are cases where "0" will be a valid input
    – Stumbler
    Jun 30, 2015 at 19:11

4 Answers 4

5

The problem is in your logic, you check if $_POST[whatever] isset, and if so, you then output "Variable is NOT set" this is the opposite of what you're if statement checks. Try adding a ! in front of isset:

if (!isset($_POST[$variable_name])) {
    echo 'Variable: ' . $variable_name . ' is not set<br/>';
    array_push($error_array, " " . $variable_name);
}
9
  • Although I was a complete idiot for not spotting this; also required || ($_POST[$variable_name]=="") to catch all true positives
    – Stumbler
    Jun 30, 2015 at 19:20
  • the op's output is wrong the logic is correct, read this part of the question "Why are these elements incorrectly registering as being set" the OP wondering whey they are set and is checking if they are set. the text output should read "is set" Jun 30, 2015 at 19:20
  • Do people really read the questions? or is it all a joke I wonder. Jun 30, 2015 at 19:23
  • @ArtisiticPhoenix The message says "X is not set" if and only if it IS indeed set. PS. its possible for it not to be set, if the script is accessed outside of the form its supposed to come from (AKA always code defensively, as the OP is trying to do)
    – Derokorian
    Jun 30, 2015 at 19:23
  • @Derokorian - the question is clearly why are they set, which they are - flipping the logic doesnt unset them. Granted they are not set in the case of the form not being submitted but that is not the question. 'This results, in the submission of a blank form, in the output of' AND 'Why are these elements incorrectly registering as being set' Jun 30, 2015 at 19:25
0

try making sure the trimmed value is not an empty string and that the variable exists

foreach ($variables as $variable_name) {

    if (isset($_POST[$variable_name]) && "" != trim($_POST[$variable_name]) ) {
        echo 'Variable: ' . $variable_name . ' is not set<br/>';
        array_push($error_array, " " . $variable_name);
    }
}
0
if (isset($_POST[$variable_name])) {
    echo 'Variable: ' . $variable_name . ' is set!!<br/>';
} elseif(!isset($_POST[$variable_name])){
    array_push($error_array, " " . $variable_name);
} elseif( empty($_POST[$variable_name])){
    echo 'Variable: ' . $variable_name . ' is empty!!<br/>';
} else{
   var_dump($_POST[$variable_name])
}
1
  • check all post variable before validatel, a function like: public function trimArray($data){ $arg = array(); foreach($data as $k=>$v){ $arg[$k] = trim($v); } return $arg; } public function stripTagsArray($data){ $arg = array(); foreach($data as $k=>$v){ $arg[$k] = strip_tags($v); } return $arg; } public function praparePost($data){ $prapare1 = $this->trimArray($data); $prapare2 = $this->stripTagsArray($prapare1); return $prepare2; }
    – rafaelphp
    Jun 30, 2015 at 19:16
0

Hi there is no need to check if isset because it will always be set you only need to check for this

if( trim($_POST['key']) != '' ){

}

Also if you really need to use the isset

$post = array_filter( array_map('trim', $_POST), function($a){
      return $a != '';
});

That will remove them from $post

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.