I need to check if $_POST
variables exist using single statement isset.
if (isset$_POST[\'name\'] && isset$_POST[\'number\'] &&am
Do you need the condition to be met if any of them are set or all?
foreach ($_POST as $var){
if (isset($var)) {
}
}
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');
if isset(($_POST['name']) && ($_POST['number']) && ($_POST['address']))
You can also use this. it might be more easy.
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';
Use Array to collect data from form as follow:
and process your form on post as below:
if(isset($_POST['name'])){
...
}
Old post but always useful
foreach ($_POST as $key => $val){
$$key = isset($_POST[$key]) ? $_POST[$key] : '';
}