tôi chỉ viết một chức năng nhanh chóng để làm điều này. Tôi cần nó để xử lý nhiều hình thức vì vậy tôi đã làm cho nó để nó sẽ chấp nhận một chuỗi cách nhau bởi ','.
//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;
}
Vì vậy, bạn có thể nhập hàm này vào mã của mình và xử lý lỗi trên mỗi trang.
$postError = errorPOSTEmpty('login,password,confirm,name,phone,email');
if ($postError === true) {
...error code...
} else {
...vars set goto POSTing code...
}
Một lần nữa, tôi đề nghị một isSet ($ _ POST [$ field]). Đây là một giải pháp tốt, mặc dù. – Borealid
Cảm ơn Harold, đó là những gì tôi đang tìm kiếm .. – FFish
trống() kiểm tra cả hai giá trị tồn tại và không giả mạo (null, false, 0, chuỗi rỗng). –