Check for all POST variables

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 go into big forms? Will they continue to be repeated until all form input has been set using ['key']?

+5
source share
2 answers

You can also write:

 <?php if (isset($_POST) && $_POST != NULL ){ foreach ($_POST as $key => $value) { // perform validation of each item } ?> 

Here is 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 forged your form and added the e field, then the previous code did not accept the value from the e field.

0
source

you can 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 the array is returned to the result array. Array keys are saved.

0
source

Source: https://habr.com/ru/post/1208923/


All Articles