dongxing7083 2015-01-10 16:04
浏览 36
已采纳

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)

  • 写回答

3条回答 默认 最新

  • dongqi8030 2015-01-10 16:09
    关注

    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) "" }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度