How to check the $ _POST multiple variable for existence using isset ()?

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

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

is there any easy way to achieve this?

+4
source share
9 answers

Use the easy way with array_diff and array_keys

 $check_array = array('key1', 'key2', 'key3'); if (!array_diff($check_array, array_keys($_POST))) echo 'all exists'; 
+14
source
 $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, iterating 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 the easiest way - if any of your $_POST variables changes, you don't need to update the array with new names.

+5
source

Do you need a condition that must be met if any of them are set or all?

 foreach ($_POST as $var){ if (isset($var)) { } } 
+3
source
 $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 } } } 
+1
source

What you are asking is exactly what is in the isset page

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

matches with:

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

If you are asking for a better or practical way of saying this, assuming that you already have all the necessary keys, you can use something like:

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

Remember that isset does not return the same result as array_key_exists

+1
source

The following is a custom function that takes an array for the required published elements as a parameter and returns true if all of them are sent, and none of them is an empty string '' or false if there is at least one of them 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'); 
0
source

Use an array to collect data from the form as follows:

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

and process the form in the message as shown below:

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

Old post but always useful

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

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

You can also use this. it might be easier.

-2
source

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


All Articles