PHP - 不应该'if(!$ _ POST)'足够(不必指定字段名称)来检测数据是否通过带有1个字段的表单传递?

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)

when posting like this and having empty fields, it still gets saved as value.

var_dumping this

<form method="post" action="fgc.php">
<input type="text" name="horse">
<input type="submit">
</form>

will return:

array(1) { ["horse"]=> string(0) "" }

try this code

if($_SERVER['REQUEST_METHOD'] == 'POST'){ // if the form was submitted

  if(isset($_POST['name']) && !empty($_POST['name'])){ // basic validation

   // # Don't forget XSS sanitizing !
   $name = htmlspecialchars($_POST['name']);

   // Add data to DB or something

  }else{
   echo 'Error: Required field "name"';
  }

}

In PHP, the $_POST superglobal is always defined, regardless of whether or not the method was actually POST, or if any data was posted. However, if it isn't a POST, or if there is no data, that array will be empty.

If you convert an array to a boolean, the value will be false if there are no elements in the array.

<?php
var_dump(isset($_POST)); // Always TRUE
var_dump(!!$_POST); // TRUE if data was posted (even if empty fields), FALSE otherwise

The reason your documentation says to use !$_POST is that often times the page will be loaded with the GET method, in which case, $_POST will be an empty array.