Conditions do not return expected values

Asked

Viewed 99 times

1

I have a simple HTML and php application, which consists of 2 fields, where they will be received in a php page by the method $_POST. If I add the values to the field, it goes correctly. However if I didn’t add anything, I hoped it would fall into "There was no Ubmit", but he always throws me to "Fill the fields".

I believe it’s a problem with us else but I couldn’t 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";





?>
  • 1

    Apparently it is correct, because if you do not fill in the fields and send, still it will be a request, where the variable $_POST will not be empty and only the fields name and Qtde inside it that will be.

  • I think you’re right Yure.

3 answers

3

The flow is correct because the 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 you submit, it will result in:

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

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

To create a condition of "there was no Submit", there are several solutions.

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

if (isset($_POST) && count($_POST)>0)
  • +1 exactly that,"See that $_POST is not empty, but the objects inside it are."

  • The answer of the expert is always simpler and objective. + 1

2


It took me a while to finish the answer, and I haven’t looked at the others. I did more because for me it represented an "achievable challenge", and even knowing that there are people much better than me to answer, I’ll take my chances in this. :)

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

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 she is empty, Write: "There was no Submit".

So the only way you can get the answer "There was no Ubmit," is if the variable $_POST contain nothing within it, and when you send the information through the button submit, 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 $_POST variable is not empty, it contains something. Therefore, the only way to get this result is to not send the submit.

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

Regarding the other conditions, it seems you made a mess with else and else if, and logic itself does not seem to suit what the answers (the echos) inform.

So what I’ve done is recreate the conditions for them to reflect what the answers say.

The first message is successful, and requires that:

  • both variables are set (the respective fields exist);
  • both variables are filled (not empty);

Then you need 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 on this, and I’m listing some to the end).

So this condition was 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 must be set but empty.

Then the condition is 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 we can better understand the difference between the isset and the empty.

Note that, according to the message, the name field there is no in $_POST, then the condition is 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 name field in the form, so it is not found by $_POST:

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

Thus, you meet the above condition (i.e.: not set - in !isset the ! means não)

You also need to change the answer text 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 question.

Follow the standard code:

html test.:

<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>

php students.:

<?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 on Ideone, where the message is just the one you wanted, because in the case there is no submit.

Related questions:

What is the difference between Else and elseif?

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

0

Try it like this :

if (count($_POST) > 0) 

The point is that the empty() is not checking correctly and returning that there is something in the array when it does not exist.

See working :Ideone

Browser other questions tagged

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