5

What are some indistury standards for checking if all $_POST values are set. I know you can do the following:

if(isset($_POST['name']) && isset($_POST['username']))

But what happens when you run in to large forms? Would they just keep on repeating until all of the form inputs havr been set via the ['key']?

  • 1
    You could make a function or a loop. – Rizier123 Dec 12 '14 at 22:06
  • 1
    Exactly like your example. – Madbreaks Dec 12 '14 at 22:06
  • Not an industry standard, but I'd use `$_POST->has("name", "id", "user")` or something. Most frameworks provide a means to specify and check for required fields. – mario Dec 12 '14 at 22:09
  • 1
    Check this out: http://stackoverflow.com/questions/4496276/check-all-post-variable-at-once – Rizier123 Dec 12 '14 at 22:11
  • For a large form I would suggest making an array of possible fields that come through post and some stats about them such as required, data type, perhaps a validation function...etc. Then looping over that list to check that the post value has come through (if required) and validate what does come through. – Jonathan Kuhn Dec 12 '14 at 22:12
  • Thank you Rizier, that seems like an easier method. –  Dec 12 '14 at 22:13
  • @RandomGuy Your welcome! Have a nice day :D (Well for me there is only 50min left on the day xD) – Rizier123 Dec 12 '14 at 22:14

2 Answers2

0

You may also write:

<?php
if (isset($_POST) && $_POST != NULL ){

  foreach ($_POST as $key => $value) {

        // perform validation of each item
  }
?>

Here's a more detailed example:

<?php
if (isset($_POST) && $_POST != NULL) {

$clean = array();

foreach ($_POST as $key => $value) {
    switch($key)
    {
    case "a":
      if (ctype_digit($value)){
        $clean[$key] = $value;      
      } break;
    case "b":
      if ( ctype_alpha($value)){
        $clean[$key] = $value;      
      } break;
    case "c":
      if ( ctype_alnum($value)){
         $clean[$key] = $value;     
      } break;
    case "d": 
      if (ctype_punct($value)){
         $clean[$key] = $value;     
      } break;
     default: echo $value, " is invalid data\n";
    }
}
var_dump($clean);

}

?>

So, if you had a form with input fields a,b,c,d and someone spoofed your form and added a field e, then the preceding code would not accept the value from field e.

slevy1
  • 3,797
  • 2
  • 27
  • 33
0

you may use this function:

if(!array_filter($_POST)) {
   //echo somthing to user to fill the form
}

array_filter Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.

Peyman.H
  • 1,819
  • 1
  • 16
  • 26
  • This would check if there was at least one post value without a falsey value sent. I believe OP is talking more about for validation, not looking to see if $_POST is empty. – Jonathan Kuhn Dec 12 '14 at 22:15
  • @JonathanKuhn he doesnt say anything about validation!He talks about checking for whether the variables are set or not – Peyman.H Dec 12 '14 at 22:17
  • But in the code example, he is looking for specific keys to make sure they are set. Else, why wouldn't they just use `if(!empty($_POST))` or the more reliable `if($_SERVER['REQUEST_METHOD'] == 'POST')` And by validation, I assume it is more likely for required fields, just making sure they are set since they are looking for specific fields. – Jonathan Kuhn Dec 12 '14 at 22:21
  • Also, if no callback is passed then it removes any values that are falsey (non type sensitive false `==`) from the return array which might include perfectly valid values like if a form submitted a blank string or 0. – Jonathan Kuhn Dec 12 '14 at 22:26
  • @JonathanKuhn you are totally true. I just mention this method as one of the many methods someone can use. It's just a suggestion.:) – Peyman.H Dec 12 '14 at 22:26