PHP: check if any posted vars are empty - form: all fields required
I use my own custom function...
public function areNull() {
if (func_num_args() == 0) return false;
$arguments = func_get_args();
foreach ($arguments as $argument):
if (is_null($argument)) return true;
endforeach;
return false;
}
$var = areNull("username", "password", "etc");
I'm sure it can easily be changed for you scenario. Basically it returns true if any of the values are NULL, so you could change it to empty or whatever.
if( isset( $_POST['login'] ) && strlen( $_POST['login'] ))
{
// valid $_POST['login'] is set and its value is greater than zero
}
else
{
//error either $_POST['login'] is not set or $_POST['login'] is empty form field
}
Something like this:
// Required field names
$required = array('login', 'password', 'confirm', 'name', 'phone', 'email');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
} else {
echo "Proceed...";
}