php - Foreach loop that loops through $_POST data instead of static isset($_POST["name"]......) -


so here again trying find better ways of doing things. 90% of tutorials things normal way below:

if (isset($_post['name']) && isset($_post['password'])) {      // stuff...     } 

it fine seem static since prefer far more dynamic. example lets looping through $_post arrays within contact form. way can change name or fields whatever want or add more...my code handle rest.

i know foreach loop come in handy but, new world of programming , php thought show me how done. how replace above loop? not sure start.

try this:

$check=true;    if(isset($_post)){   foreach($_post $key=>$value){    if(!isset($_post[$key]){     $check = false;     break;      }    } } 

based on $check can verify if sent or not.

another approach have sort of verification because possible might not key in $_post

$keys =array("input1","input2");  $check=true;    if(isset($_post)){   foreach($keys $input){    if(!array_key_exists($input,$_post)){     $check = false;     break;      }    } } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -