2010-07-06 23 views

Trả lời

58

Something như thế này ($ đăng nhập || $ password!):

// 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..."; 
} 
+3

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

+0

Cảm ơn Harold, đó là những gì tôi đang tìm kiếm .. – FFish

+8

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). –

1

emptyisset nên làm điều đó.

if(!isset($_POST['submit'])) exit(); 

$vars = array('login', 'password','confirm', 'name', 'email', 'phone'); 
$verified = TRUE; 
foreach($vars as $v) { 
    if(!isset($_POST[$v]) || empty($_POST[$v])) { 
     $verified = FALSE; 
    } 
} 
if(!$verified) { 
    //error here... 
    exit(); 
} 
//process here... 
+0

Bạn cũng cần một isSet, tôi nghĩ - nếu không bạn sẽ gặp lỗi nếu giá trị đó không được đăng. – Borealid

0

Cá nhân tôi trích xuất các mảng POST và sau đó có nếu sau đó echo điền vào mẫu :)

+2

Awww, luôn luôn là một thực tế nguy hiểm, bởi vì nó có thể có thể buôn lậu các biến toàn cầu vào kịch bản của bạn thông qua '$ _POST'. –

+0

Tôi đã nghe điều gì đó về điều này trước đây, và nó có lẽ không phải là cách tốt nhất để đi :) đặc biệt là nếu một cái gì đó quan trọng của nó –

3

Tôi sử dụng chức năng tùy chỉnh của riêng mình ...

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"); 

Tôi chắc chắn rằng nó có thể dễ dàng thay đổi cho bạn. Về cơ bản nó trả về true nếu bất kỳ giá trị nào là NULL, vì vậy bạn có thể thay đổi nó thành rỗng hoặc bất cứ thứ gì.

1
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 
} 
+0

nếu bạn có một trường trống được gửi thì strlen() sẽ đánh giá là 0 là false trong PHP. –

0

tôi đã làm nó như thế này:

$missing = array(); 
foreach ($_POST as $key => $value) { if ($value == "") { array_push($missing, $key);}} 
if (count($missing) > 0) { 
    echo "Required fields found empty: "; 
    foreach ($missing as $k => $v) { echo $v." ";} 
    } else { 
    unset($missing); 
    // do your stuff here with the $_POST 
    } 
0

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... 
} 
0

Lưu ý: Chỉ cần cẩn thận nếu 0 là giá trị chấp nhận được cho trường bắt buộc. Như @ Harold1983- đã đề cập, chúng được coi là rỗng trong PHP. Đối với những điều này, chúng tôi nên sử dụng isset thay vì trống.

$requestArr = $_POST['data']// Requested data 
$requiredFields = ['emailType', 'emailSubtype']; 
$missigFields = $this->checkRequiredFields($requiredFields, $requestArr); 

if ($missigFields) { 
    $errorMsg = 'Following parmeters are mandatory: ' . $missigFields; 
    return $errorMsg; 
} 

// Function to check whether the required params is exists in the array or not. 
private function checkRequiredFields($requiredFields, $requestArr) { 
    $missigFields = []; 
    // Loop over the required fields and check whether the value is exist or not in the request params. 
    foreach ($requiredFields as $field) {`enter code here` 
     if (empty($requestArr[$field])) { 
      array_push($missigFields, $field); 
     } 
    } 
    $missigFields = implode(', ', $missigFields); 
    return $missigFields; 
} 
Các vấn đề liên quan