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