Hướng dẫn how can we check input type is not empty in php? - Làm thế nào chúng ta có thể kiểm tra loại đầu vào không trống trong php?

Có một chức năng đơn giản hơn đối với một cái gì đó như thế này:

if (isset($_POST['Submit'])) {
    if ($_POST['login'] == "" || $_POST['password'] == "" || $_POST['confirm'] == "" || $_POST['name'] == "" || $_POST['phone'] == "" || $_POST['email'] == "") {
        echo "error: all fields are required";
    } else {
        echo "proceed...";
    }
}

Hướng dẫn how can we check input type is not empty in php? - Làm thế nào chúng ta có thể kiểm tra loại đầu vào không trống trong php?

Taryn

Phim thương hiệu vàng 238K5555 gold badges362 silver badges403 bronze badges

hỏi ngày 6 tháng 7 năm 2010 lúc 21:42Jul 6, 2010 at 21:42

Một cái gì đó như thế này:

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

Đã trả lời ngày 6 tháng 7 năm 2010 lúc 21:46Jul 6, 2010 at 21:46

Harold1983-Harold1983-Harold1983-

3.2392 Huy hiệu vàng22 Huy hiệu bạc22 Huy hiệu đồng2 gold badges22 silver badges22 bronze badges

8

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
}

Hướng dẫn how can we check input type is not empty in php? - Làm thế nào chúng ta có thể kiểm tra loại đầu vào không trống trong php?

Đã trả lời ngày 18 tháng 9 năm 2012 lúc 21:54Sep 18, 2012 at 21:54

Nahser bakhtnahser bakhtNahser Bakht

9102 Huy hiệu vàng13 Huy hiệu bạc26 Huy hiệu đồng2 gold badges13 silver badges26 bronze badges

2

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 kịch bản của bạn. Về cơ bản, nó trả về đúng nếu bất kỳ giá trị nào là null, vì vậy bạn có thể thay đổi nó thành trống hoặc bất cứ điều gì.

Đã trả lời ngày 6 tháng 7 năm 2010 lúc 21:50Jul 6, 2010 at 21:50

Hướng dẫn how can we check input type is not empty in php? - Làm thế nào chúng ta có thể kiểm tra loại đầu vào không trống trong php?

Animuson ♦ Animusonanimuson

52.8K28 Huy hiệu vàng139 Huy hiệu bạc145 Huy hiệu đồng28 gold badges139 silver badges145 bronze badges

// 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...";
}
0 và
// 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...";
}
1 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...

Đã trả lời ngày 6 tháng 7 năm 2010 lúc 21:44Jul 6, 2010 at 21:44

Jacob Relkinjacob RelkinJacob Relkin

158K32 Huy hiệu vàng341 Huy hiệu bạc318 Huy hiệu đồng32 gold badges341 silver badges318 bronze badges

2

Tôi đã làm 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
  }

Đã trả lời ngày 4 tháng 11 năm 2013 lúc 0:03Nov 4, 2013 at 0:03

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 để tôi thực hiện nó để nó 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 chức năng này vào mã của mình và xử lý các lỗi trên cơ sở trên mỗi trang.

$postError = errorPOSTEmpty('login,password,confirm,name,phone,email');

if ($postError === true) {
  ...error code...
} else {
  ...vars set goto POSTing code...
}

Đã trả lời ngày 3 tháng 2 năm 2015 lúc 0:41Feb 3, 2015 at 0:41

user1518699user1518699user1518699

3872 Huy hiệu vàng8 Huy hiệu bạc19 Huy hiệu đồng2 gold badges8 silver badges19 bronze badges

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

$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;
}

Đã trả lời ngày 27 tháng 12 năm 2017 lúc 9:22Dec 27, 2017 at 9:22

Hướng dẫn how can we check input type is not empty in php? - Làm thế nào chúng ta có thể kiểm tra loại đầu vào không trống trong php?

foreach($_POST as $key=>$value)
{

   if(empty(trim($value))
        echo "$key input required of value ";

}

Đã trả lời ngày 13 tháng 8 năm 2019 lúc 18:21Aug 13, 2019 at 18:21

Dılo Sürücüdılo Sürücüdılo sürücü

3.1881 Huy hiệu vàng18 Huy hiệu bạc26 Huy hiệu đồng1 gold badge18 silver badges26 bronze badges

5

Cá nhân tôi trích xuất mảng bài và sau đó có nếu (! $ Đăng nhập |

Đã trả lời ngày 6 tháng 7 năm 2010 lúc 21:45Jul 6, 2010 at 21:45

Hướng dẫn how can we check input type is not empty in php? - Làm thế nào chúng ta có thể kiểm tra loại đầu vào không trống trong php?

Doug Molineuxdoug MolineuxDoug Molineux

12.1k25 Huy hiệu vàng89 Huy hiệu bạc142 Huy hiệu đồng25 gold badges89 silver badges142 bronze badges

2

Làm thế nào để bạn kiểm tra xem một dòng trống trong PHP?

Trả lời: Sử dụng hàm php clan () Bạn có thể sử dụng hàm pHP clan () để tìm hiểu xem một biến có trống hay không. Một biến được coi là trống nếu nó không tồn tại hoặc nếu giá trị của nó bằng sai.Use the PHP empty() function You can use the PHP empty() function to find out whether a variable is empty or not. A variable is considered empty if it does not exist or if its value equals FALSE .

Cái gì không trống trong PHP?

Hàm php trống () được sử dụng để kiểm tra xem một biến có trống hay không. Để kiểm tra xem biến PHP không trống, bạn có thể gọi hàm trống bị phủ định hoặc! Trống () trong câu lệnh IF của bạn.call the negated empty function or ! empty() in your if statement.

Làm thế nào để bạn kiểm tra nếu một biểu mẫu trống?

Kiểm tra trường không trống..
Hàm JavaScript để kiểm tra xem trường có trống hay không // Nếu độ dài của chuỗi phần tử là 0 hay không, hiển thị hàm tin nhắn trợ giúp (inputTx) {if (inputTx.Value.length == 0) {alert ("message");trả lại sai;} trả về đúng;} ....
Flowchart:.
Mã HTML

NULL hay PHP trống?

is_null () hàm trống () trả về true nếu giá trị của một biến đánh giá là sai.Điều này có thể có nghĩa là chuỗi trống, null, số nguyên 0 hoặc một mảng không có phần tử.Mặt khác, is_null () sẽ chỉ trả về true nếu biến có giá trị null.The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .