$_POST 行为奇怪 - 它与预期的相反?

$_POST behavior strange - it works opposite as expected?

我有下面的 html 表格并将其提交到 php 文件 (workcard_done.php)。我的问题是当我提交代码时它回显 else echo "nothing here"; 如果我在 isset 上设置 not (!) 运算符,我会得到想要的结果吗?它应该与 if(isset($_POST['submit']) && !empty($_POST['submit'])) 一起正常工作。

谁能告诉我下面的代码有什么问题,这让我抓狂。

<!DOCTYPE html>
<HTML>
<head>
<title>yoyo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form action="workcard_done.php" name="workCard_open" method="POST">
<br>
<table>
<tr>
<th>title 1</th>
<th>title 2</th>
</tr>
<tr>
<td><input type="text" name="name[]" value="" ></td>
<td><input type="text" name="price[]" value="" class="matr_price"/></td>
</tr>
<tr>
<td><input type="text" name="name[]" value="" ></td>
<td><input type="text" name="price[]" value="" class="matr_price"/></td>
</tr>
<tr>
<td>Total incl. VAT:</td>
<td><span type="text" name"sum[]" id="sum">0</span></td>
</tr>
</table>
<input type="submit" name"submit" class="submit" value="Done"/>
</form>
</body>
</html>

workcard_done.php

<!DOCTYPE html>
<html>
<head>
<title>title...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<pre>
<?php
error_reporting(E_ALL);
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
  $name = $_POST['name'];
  $price = $_POST['price'];
  $user = $_POST['user'];

  print_r($name);
  echo "<br>";  
  print_r($price);
  echo "<br>";
  print_r($user);
  echo "<br>";
}
else {
  echo "nothing here";
}
?>
</pre>
</body>
</html>

如果在 isset 上设置非 (!) 运算符,我会得到以下想要的结果。

Array
(
    [0] => slange
    [1] => rfd
)
Array
(
    [0] => 123
    [1] => 200
)
Array
(
    [user] => 2
)

您在 <input type="submit" name"submit"

上错过了一个 =

检查 HTML 的这一行:

<input type="submit" name"submit" class="submit" value="Done"/>

提交按钮没有名称,这就是 isset($_POST['submit']) == FALSE 的原因。在属于它的地方加上等号(=),突然间你的问题就会消失。

检查 $_POST,而不是 $_POST['submit']。

如果用户按下回车键怎么办?提交按钮永远不会触发,该值将不存在。

顺便别忘了CRSF令牌。

您忘记了 = 作为其他答案的状态,但您也在尝试访问 $_POST['user'],而 none 的输入字段名称为 user .