so I've got the latest version of Xampp installed and in the htdocs directory I have an htm page containing a very simple form with just a single text field and a submit button which, when clicked, links to a php page that prints a message saying "What you typed is:" followed by what was typed. If nothing was actually typed in the field, after clicking on the submit button, the php page will display an error message saying "Error: you didn't type anything".
Here is the code for the htm page:
<html>
<head>
</head>
<body>
<center>
<form action="p1.php" method="post">
Type something:
<input type="text" name="nom">
<input type="submit" value="SEND">
</form>
</center>
</body>
</html>
And here is the initial code for the php page:
<html>
<head>
</head>
<body>
<?PHP
if (!$_POST) {echo "Error: you didn't type anything";}
else {echo "What you typed is: " . $_POST["nom"];}
?>
</body>
</html>
So with this php code, if I type anything in the field and click the submit button, the php page will display "What you typed is:" followed by what was typed but if I don't actually type anything in the field and click the submit button, the php page will display "What you typed is:" followed by nothing instead of displaying "Error: you didn't type anything".
However, I discovered that if I changed the "if (!$_POST)" to "if (!$_POST["nom"])", then if I didn't type anything in the field, the php page would display "Error: you didn't type anything"...problem solved.
But this surprised me, as I have seen in my course material an example (it is referred as a self-calling form or something along those lines) where "if (!$_POST)" is used.Here it is:
<html>
<head>
<title>Me llamo a mi mismo...</title>
</head>
<body>
<?
if (!$_POST){
?>
<form action="auto-llamada.php" method="post">
Nombre: <input type="text" name="nombre" size="30">
<br>
Empresa: <input type="text" name="empresa" size="30">
<br>
Telefono: <input type="text" name="telefono" size=14 value="+34 " >
<br>
<input type="submit" value="Enviar">
</form>
<?
}else{
echo "<br>Su nombre: " . $_POST["nombre"];
echo "<br>Su empresa: " . $_POST["empresa"];
echo "<br>Su Teléfono: " . $_POST["telefono"];
}
?>
</body>
</html>
So why isn't "if (!$_POST)" not working in my case? (using Mozilla as the browser)