Home > Net >  PHP: Check if $_POST-Array contains empty/not set values?
PHP: Check if $_POST-Array contains empty/not set values?

Time:03-23

Imagine a $_POST-Array from a form different input-types/elements. I want to iterate over the Array and check if the user has filled out all fields or chose an non-default option from all select-dropdowns.

In the code below I tried with the function postContainsEmptyValues(), but this does not work for the select fields (Edit: It works for the text-inputs).

How do I have to change the code?

(EDIT: updated the code according to brombeers hint (removing disabled))

<?php
if (isset($_POST["btSubmit"])) {
  if (postContainsEmptyValues()) {
    echo "empty form element detected";
  } else {
    echo "all form-fields have been filled";
  }
}
function postContainsEmptyValues()
{
  $emptyElementDetected = false;
  foreach ($_POST as $element) {
    if ($element == "" || !isset($element)) {
      $emptyElementDetected = true;
    }
    return $emptyElementDetected;
  }
}
?>
<form action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>" method="POST">
    <select name="selectBox1">
        <option value="" selected>-- choose one --</option>
        <option value="a">choose a</option>
        <option value="b">choose b</option>
    </select>
    <select name="selectBox2">
        <option value="" selected>-- choose one --</option>
        <option value="x">choose x</option>
        <option value="y">choose y</option>
    </select>
    <input type="text" name="tfText" value="">
    <input type="submit" name="btSubmit">
</form>

CodePudding user response:

Disabled form elements/options won't be sent when submitting the form. Remove the disabled attribute from your <option>s:

<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
  <select name="selectBox1">
    <option value="" selected>-- choose one --</option>
    <option value="a">choose a</option>
    <option value="b">choose b</option>
  </select>
  <select name="selectBox2">
    <option value="" selected>-- choose one --</option>
    <option value="x">choose x</option>
    <option value="y">choose y</option>
  </select>
  <input type="text" name="tfText" value="">
  <input type="submit" name="btSubmit">
</form>

From https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled: "The Boolean disabled attribute, when present, makes the element not mutable, focusable, or even submitted with the form."

Edit: you're returning inside the foreach loop, so only the first item will be checked. Move the return outside of the loop. Complete working code:

<?php
if(isset($_POST['btSubmit'])) { 
    if (postContainsEmptyValues()) {
        echo "empty form element detected";
    }
}

function postContainsEmptyValues()
{
    $emptyElementDetected = false;
    foreach ($_POST as $element) {
        if ($element == "" || !isset($element)) {
           $emptyElementDetected = true;
        }
    }
    return $emptyElementDetected;
}
?>

<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
  <select name="selectBox1">
    <option value="" selected>-- choose one --</option>
    <option value="a">choose a</option>
    <option value="b">choose b</option>
  </select>
  <select name="selectBox2">
    <option value="" selected>-- choose one --</option>
    <option value="x">choose x</option>
    <option value="y">choose y</option>
  </select>
  <input type="text" name="tfText" value="">
  <input type="submit" name="btSubmit">
</form>

A slightly shorter/more performant version of your function (this returns right away when an empty element has been found, no need to check all elements):

function postContainsEmptyValues()
{
    foreach ($_POST as $element) {
        if (empty($element)) {
            return true;
        }
    }
    return false;
}

CodePudding user response:

Your function returns true if there's a missing element, but you are echoing empty element found if the function return false

  • Related