PHP: check if any posted vars are empty - form: all fields required

后端 未结 9 1561
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 15:16

Is there a simpler function to something like this:

if (isset($_POST[\'Submit\'])) {
    if ($_POST[\'login\'] == \"\" || $_POST[\'password\'] == \"\" || $_P         


        
9条回答
  •  感情败类
    2020-11-27 15:59

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

提交回复
热议问题