Właśnie napisałem funkcję szybkiego, aby to zrobić. Potrzebowałem go do obsługi wielu formularzy, więc zrobiłem to, aby zaakceptować łańcuch oddzielony przez ",".
//function to make sure that all of the required fields of a post are sent. Returns True for error and False for NO error
//accepts a string that is then parsed by "," into an array. The array is then checked for empty values.
function errorPOSTEmpty($stringOfFields) {
$error = false;
if(!empty($stringOfFields)) {
// Required field names
$required = explode(',',$stringOfFields);
// Loop over field names
foreach($required as $field) {
// Make sure each one exists and is not empty
if (empty($_POST[$field])) {
$error = true;
// No need to continue loop if 1 is found.
break;
}
}
}
return $error;
}
Więc można wprowadzić tę funkcję w kodzie i obsługi błędów w przeliczeniu na stronę podstawie.
$postError = errorPOSTEmpty('login,password,confirm,name,phone,email');
if ($postError === true) {
...error code...
} else {
...vars set goto POSTing code...
}
Ponownie, polecam isSet ($ _ POST [$ field]). To dobre rozwiązanie. – Borealid
Dzięki Harold, właśnie tego szukałem .. – FFish
empty() sprawdza zarówno istnienie, jak i wartości innych niż false (null, false, 0, pusty ciąg znaków). –