Conditions do not return expected values

1

I have a simple HTML and php application, which consists of 2 fields, where they will be received on a php page by the $_POST method. If I add the values to the field, it goes correctly. But if I did not add anything, I expected it to fall into "No submit", but it always throws me to "Fill in the fields."

I think it's a problem with else but I could not find where.

<?php

     if (!empty($_POST)) {
         if ((isset($_POST['nome'])) && (isset($_POST['qtde']))) {
             if ((!empty($_POST['nome'])) && (!empty($_POST['qtde']))) {
                 $nome = $_POST['nome'];
                 $qtde = $_POST['qtde'];
                 echo "Aviso ao representate $nome <br/>
         A equipe com $qtde alunos foi aceita!";

             } else echo "Preencha os campos";

         } else echo "O campo [nome] nao existe na variavel $_POST";


     }else
         echo "não houve submit";





?>
    
asked by anonymous 13.11.2015 / 19:05

3 answers

2

It took me a while to finish the answer, and I have not looked at the others yet. I did more because it represented an "achievable challenge" for me, and even though I know that there are people that are better than me to respond, I will take that risk. :)

Well, the first thing is to understand what each condition means in your code.

if (!empty($_POST)) {
//outras condições
}
else {
 echo "Não houve submit.";
}

This first condition means the following:

  

If the global variable $_POST is not empty , check the other conditions below. If it is empty , type: "There was no submit."

So the only way for you to get the answer "There was no submit", is if the variable $_POST does not contain anything inside it, and when you send the information through the submit button, the $ _POST variable will contain the form information.

A var_dump in $_POST without filling in the fields will return the following:

    array (size=2) 
   'nome' => string '' (length=0)
   'qtde' => string '' (length=0)

That is, the variable $ _POST is not empty, it contains something. So the only way to get this result is by not sending submit .

You can simulate this by opening the PHP script in the browser and giving enter in the navigation bar (without using the HTML page).

Regarding the other conditions, it seems that you have made a confusion with else and else if , and the logic itself does not seem to match what the answers ( echos ) report.

So what I did was re-create the conditions to reflect what the answers tell.

The first message succeeds, and requires that:

  • Both variables are set (their respective fields exist);
  • Both variables are filled (not empty);
So I have to understand the difference between set ( isset ) and empty ( empty ), and I think this will be clear in the last condition (and there are several very good topics here about this, final).

So this condition looks like this:

if (isset($_POST['nome']) && isset($_POST['qtde']) &&
        !empty($_POST['nome']) && !empty($_POST['qtde'])) {
            $nome = $_POST['nome'];
            $qtde = $_POST['qtde'];
            echo "Aviso ao representate $nome <br/>
         A equipe com $qtde alunos foi aceita!";
    }

The next condition is "Fill the fields", which means they should be set, but empty.

Then the condition looks like this:

else if (isset($_POST['nome']) && isset($_POST['qtde'])
       && empty($_POST['nome']) && empty($_POST['qtde'])) {

                echo "Preencha os campos";
            }

Now in this last condition ("The field [name] does not exist in the variable $ _POST") I think you can better understand the difference between isset and empty .

Note that the message name does not exist in $_POST , so the condition looks like this:

else if (!isset($_POST['nome'])) {

            echo "O campo nome nao existe na variável POST.";
    }

Now, for you to get this message, you have to change the name of the name field on the form, so that it is not found by $_POST :

<input type="text" name="name111">

So, you meet the above condition (ie: is not set - in !isset o ! means não )

You also need to change the text of the response to POST only, because with $_POST will give error, since it is a array . Or you can escape the characters, but the answer is already great and I will not go into this.

Follow the standard code:

test.html:

<form action="alunos.php" method="post">
    <label for="nome">
        <input type="text" name="nme" id="nome">
    </label>
    <label for="qtde">
        <input type="number" id="qtde" name="qtde">
    </label>
    <label for="submit">
        <input type="submit" value="Enviar" id="submit"/>
    </label>
</form>

Students.php:

<?php

if (!empty($_POST)) {

    if (isset($_POST['nome']) && isset($_POST['qtde']) &&
        !empty($_POST['nome']) && !empty($_POST['qtde'])) {

            $nome = $_POST['nome'];
            $qtde = $_POST['qtde'];
            echo "Aviso ao representate $nome <br/>
         A equipe com $qtde alunos foi aceita!";
    }
        else if (!isset($_POST['nome'])) {

            echo "O campo nome nao existe na variável POST.";
    }
        else if (isset($_POST['nome']) && isset($_POST['qtde'])
       && empty($_POST['nome']) && empty($_POST['qtde'])) {

                echo "Preencha os campos";
            }
    }

else {

    echo "Não houve submit.";

}

See working in Ideone , where the message is just the one you wanted, because in the case there is no submit .

Related questions:

What's the difference between elseif and elseif? / a>

(I know there are others, then I'll look for ...)

    
13.11.2015 / 21:00
3

The flow is correct because POST will exist as an array of objects even if the fields are empty. So the flow goes through empty($_POST) .

<?php
if (isset($_POST))
    var_dump($_POST);
    var_dump(empty($_POST));
?>
<form action="tmp.php" method="POST">
<input type="text" name="foo" value="" size="20" />
<input type="submit" value="send" />
</form>

When submitting, it will result in this:

array(1) {
  ["foo"]=>
  string(0) ""
}
bool(false)

See that $_POST is not empty, but the objects inside it are.

To create a "no-submit" condition, there are several solutions.

A simpler, more logical solution is to check the size of the array instead of checking if it is empty.

if (isset($_POST) && count($_POST)>0)
    
13.11.2015 / 19:19
0

Try this:

if (count($_POST) > 0) 

The issue is that empty() is not doing the correct verification and returning that something exists in the array, when it does not exist.

See it working: Ideone

    
13.11.2015 / 19:19