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...)
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.
– Yure Pereira
I think you’re right Yure.
– Guilherme Lima