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

后端 未结 9 1358
伪装坚强ぢ
伪装坚强ぢ 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:44

    I did it like this:

    $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 讨论(0)
  • 2020-11-27 15:46

    empty and isset should do it.

    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 讨论(0)
  • 2020-11-27 15:48

    I use my own custom function...

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

    I'm sure it can easily be changed for you scenario. Basically it returns true if any of the values are NULL, so you could change it to empty or whatever.

    0 讨论(0)
  • 2020-11-27 15:52
    foreach($_POST as $key=>$value)
    {
    
       if(empty(trim($value))
            echo "$key input required of value ";
    
    }
    
    
    0 讨论(0)
  • 2020-11-27 15:53

    I just wrote a quick function to do this. I needed it to handle many forms so I made it so it will accept a string separated by ','.

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

    So you can enter this function in your code, and handle errors on a per page basis.

    $postError = errorPOSTEmpty('login,password,confirm,name,phone,email');
    
    if ($postError === true) {
      ...error code...
    } else {
      ...vars set goto POSTing code...
    }
    
    0 讨论(0)
  • 2020-11-27 15:55
    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 讨论(0)
提交回复
热议问题