How to check if the form was sent empty

Asked

Viewed 2,684 times

1

I’m having a question how to check if the form is empty.

My intention is not to check field by field, imagining that I may have N³³³ campos.

I’m trying this way:

<form method="post" name="submit" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="input_txt"/>
    <input type="text" name="input_txt2"/>
    <input type="submit" name="sub_btn"/>
</form>
<?php
if(empty($_POST))
{
    echo "empty";
}else
{
    echo "no empty";
}
?>

However the result is always no empty.

How can I fix this ?

  • I do not know if I understand very well. If a field is filled the form would be "valid" in your doubt?

  • Like this : I have n fields, if the guy clicks on Direct Ubmit with all empty, then writes empty, if you have a data field write no empty...

  • I don’t think this is a good approach. However, I think it’s best to let someone who understands PHP answer. There’s something I don’t know about :p

  • If you have another approach to check if the form is being sent empty, without checking field by field, you are welcome...

  • I think it would be best to do so http://stackoverflow.com/a/3190482/3228982. but it is still checked field by field

  • @Magichat I don’t know if this is possible. I’ve always seen people creating a function to check if the field is null and passing a array with the field names. This is how the desired fields are checked. However, I don’t know anything that does this automatically in PHP.

  • @Augusto this link code will check if there is a blank field and not if all are blank.

  • @Pagotti, yes but you can use the same idea just by changing in if of empty for !empty and give a break. I think it would solve

Show 3 more comments

3 answers

5


It is worth mentioning that using <input name="btn" type="submit" value="Enviar" />, the value of btn will always be present in the vector $_POST, because PHP treats the button itself as a form input (since it is a input). Thus, to consider only the "real" fields of the form, it is necessary to discard such vector value:

unset($_POST["btn"]);

And then check the remaining values. It is also worth noting that a string empty is considered by PHP as a possible value and therefore the function empty will always return false, since the vector will have elements with value string empty. To get around this, simply filter the vector according to its values:

array_filter($_POST);

Not passing the second parameter of the function array_filter, all values found to be false shall be removed and in this case, string empty is, by default, considered false in PHP. This way, would remain in $_POST only duly completed fields.

The code would look something like this:

unset($_POST["sub_btn"]);

if(empty(array_filter($_POST))) {
    echo "empty";
}else {
    echo "no empty";
}
  • Very good... vlw

3

In a very simplified way:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="input_txt"/>
    <input type="text" name="input_txt2"/>
    <button name="sub_btn"/>Enviar</button>
</form>
<?php

$formVals = array_count_values($_POST);
$key = key($formVals);

if(count($formVals) == 1)
{
    echo "empty";
}else
{
    echo "no empty";
}
?>

The variable $_POST is an Array that, whenever a POST is performed, receives the values of the fields, that is, using print_r($_POST) we will get a result similar to that:

Array ( [input_txt] => [input_txt2] => [sub_btn] => )

Like [sub_btn] will always return NULL just check the amount of values present in the $_POST using array_count_values, if only 1 value (count($formVals) == 1), all fields are empty.

  • Very good man... vlw...

0

Hello But in the case of a Form so it is divided by options or will only appear to the user if he click the option button physical person only the fields physical person and if legal person only the fields legal person as would do in php the verification of all filled boxes in this case

<form action="" method="post"><div class="form-group">
      <p>
       <label class="radio-inline col-sm-4"><input type="radio" name="optradio" value="juridica" onclick="pessoa(this.value);">Pessoa Juridica</label>
        <label class="radio-inline col-sm-4"><input type="radio" name="optradio" value="fisica" onclick="pessoa(this.value);">Pessoa Fisica</label>
      </p>
      </div>

    <div id="juridica" style="display:none;">
      <div class="form-group">
        <label class="control-label col-sm-2" for="txtRazaosocial">Razão Social: *</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" id="txtRazaosocial" name="txtRazaosocial" required value="<? echo $txtRazaosocial; ?>" >
        </div>
      </div>

      <div class="form-group">
        <label class="control-label col-sm-2" for="txtNomefantasia">Nome Fantasia:</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" id="txtNomefantasia" name="txtNomefantasia" value="<? echo $txtNomefantasia; ?>">
        </div>
      </div>

       <div class="form-group">
        <label class="control-label col-sm-2" for="txtCnpj">CNPJ: *</label>
        <div class="col-sm-3">
          <input type="numbe" class="form-control cnpj" id="txtCnpj" name="txtCnpj" required value="<? echo $txtCnpj; ?>">
        </div>

        <label class="control-label col-sm-2 " for="txtIe">I.E.: * </label>
        <div class="col-sm-3">
          <input type="text" class="form-control ie" id="txtIe" name="txtIe" required value="<? echo $txtIe; ?>">
        </div>
      </div>
      </div>

      <div id="fisica" style="display:none;">
      <div class="form-group">
        <label class="control-label col-sm-2" for="txtNomePF">Nome: *</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" id="txtNomePF" required name="txtNomePF" value="<? echo $txtNomePF; ?>"><? echo $msgcamponomePF; ?>
        </div>
      </div>

       <div class="form-group">
        <label class="control-label col-sm-2" for="txtCPF">CPF: *</label>
        <div class="col-sm-3">
          <input type="numbe" class="form-control cpf" id="txtCPF" name="txtCPF" required value="<? echo $txtCPF; ?>"><? echo $msgcampoCPF;?> <? echo $msgcpf; ?>
        </div>

        <label class="control-label col-sm-2 " for="txtRG">RG:  </label>
        <div class="col-sm-3">
          <input type="text" class="form-control rg" id="txtRG" name="txtRG"  value="<? echo $txtRG; ?>" onkeypress="return SomenteNumero(event)"><? echo $msgcampoRG; ?>
        </div>
      </div>
      </div><table width="814" border="0" cellspacing="0">

I await and thank you in advance

Browser other questions tagged

You are not signed in. Login or sign up in order to post.