how to check multiple $_POST variable for existence using isset()?

前端 未结 9 730
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 05:17

I need to check if $_POST variables exist using single statement isset.

if (isset$_POST[\'name\']  &&  isset$_POST[\'number\']  &&am         


        
相关标签:
9条回答
  • 2020-12-18 05:57

    Do you need the condition to be met if any of them are set or all?

    foreach ($_POST as $var){
        if (isset($var)) {
    
        }
    }
    
    0 讨论(0)
  • 2020-12-18 06:02

    The following is a custom function that take an array for the required posted elements as a parameter and return true if they all posted and there is no any of them is empty string '' or false if there is at least one of them is not:

    function checkPosts($posts){
      if (!is_array($posts)) return "Error: Invalid argument, it should be an array";
      foreach ($posts as $post){
        if (!isset($_POST[$post]) || $_POST[$post] == '') return false;
      }
      return true;
    } 
    // The structure of the argument array may be something like:
    
    $myPosts = array('username', 'password', 'address', 'salary');
    
    0 讨论(0)
  • 2020-12-18 06:03

    if isset(($_POST['name']) && ($_POST['number']) && ($_POST['address']))

    You can also use this. it might be more easy.

    0 讨论(0)
  • 2020-12-18 06:09

    Use simple way with array_diff and array_keys

    $check_array = array('key1', 'key2', 'key3');
    if (!array_diff($check_array, array_keys($_POST)))
        echo 'all exists';
    
    0 讨论(0)
  • 2020-12-18 06:09

    Use Array to collect data from form as follow:

    • PersonArray['name],
    • PersonArray['address],
    • PersonArray['email], etc.

    and process your form on post as below:

    if(isset($_POST['name'])){
          ... 
    }
    
    0 讨论(0)
  • 2020-12-18 06:09

    Old post but always useful

    foreach ($_POST as $key => $val){
    $$key = isset($_POST[$key]) ? $_POST[$key] : '';
    }
    
    0 讨论(0)
提交回复
热议问题