how to check multiple $_POST variable for existenc

2020-02-11 06:55发布

问题:

I need to check if $_POST variables exist using single statement isset.

if (isset$_POST['name']  &&  isset$_POST['number']  &&  isset$_POST['address']  &&  etc ....)

is there any easy way to achieve this?

回答1:

Use simple way with array_diff and array_keys

$check_array = array('key1', 'key2', 'key3');
if (!array_diff($check_array, array_keys($_POST)))
    echo 'all exists';


回答2:

$variables = array('name', 'number', 'address');

foreach($variables as $variable_name){

   if(isset($_POST[$variable_name])){
      echo 'Variable: '.$variable_name.' is set<br/>';
   }else{
      echo 'Variable: '.$variable_name.' is NOT set<br/>';
   }

}

Or, Iterate through each $_POST key/pair

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

   if(isset($value)){
      echo 'Variable: '.$key.' is set to '.$value.'<br/>';
   }else{
      echo 'Variable: '.$key.' is NOT set<br/>';
   }

}

The last way is probably your easiest way - if any of your $_POST variables change you don't need to update an array with the new names.



回答3:

Do you need the condition to be met if any of them are set or all?

foreach ($_POST as $var){
    if (isset($var)) {

    }
}


回答4:

$variableToCheck = array('key1', 'key2', 'key3');

foreach($_POST AS $key => $value)
{
   if( in_array($key, $variableToCheck))
  {
     if(isset($_POST[$key])){
     // get value
     }else{
     // set validation error
    }   
  }
}


回答5:

That you are asking is exactly what is in isset page

isset($_POST['name']) && isset($_POST['number']) && isset($_POST['address'])

is the same as:

isset($_POST['name'], $_POST['number'], $_POST['address'])

If you are asking for a better or practical way to assert this considering that you already have all the required keys then you can use something like:

$requiredKeys = ['name', 'number', 'address'];
$notInPost = array_filter($requiredKeys, function ($key) {
    return ! isset($_POST[$key]);
});

Remember, isset does not return the same result as array_key_exists



回答6:

The following is a custom function that take an array for the required posted elements as a parameter and return true if they all posted and there is no any of them is empty string '' or false if there is at least one of them is not:

function checkPosts($posts){
  if (!is_array($posts)) return "Error: Invalid argument, it should be an array";
  foreach ($posts as $post){
    if (!isset($_POST[$post]) || $_POST[$post] == '') return false;
  }
  return true;
} 
// The structure of the argument array may be something like:

$myPosts = array('username', 'password', 'address', 'salary');


回答7:

Use Array to collect data from form as follow:

  • PersonArray['name],
  • PersonArray['address],
  • PersonArray['email], etc.

and process your form on post as below:

if(isset($_POST['name'])){
      ... 
}


回答8:

Old post but always useful

foreach ($_POST as $key => $val){
$$key = isset($_POST[$key]) ? $_POST[$key] : '';
}


回答9:

if isset(($_POST['name']) && ($_POST['number']) && ($_POST['address']))

You can also use this. it might be more easy.