PHP $_POST 不起作用

发布于 2024-12-20 16:20:43 字数 785 浏览 1 评论 0原文

我正在使用 PHP 开发一个 Web 应用程序。 一些我以前从未见过的错误正在发生。 $_GET 工作正常,但 $_POST 无法正常工作。想象一下下面的表单:

<form action="process.php" method="post">
   <input type="text" name="title" />
   <input type="submit" value="send" />
</form>

如您所见,我使用 post 作为表单的方法属性。在这种情况下,下面的代码将返回错误:

<?php
$sentData = $_POST['title'];
echo($sentData);    
?>

错误消息:

PHP Notice:  Undefined index: title in ...

但是如果我在 php 脚本中使用 $_GET 并在 html 表单代码中使用 get ,则一切都会正常工作,而无需任何错误。

还有更奇怪的事情。

  1. 当我使用 POST 时,只有一种形式不返回错误,其他形式返回错误。
  2. 当我在本地运行此应用程序(使用 Xampp - Apache 2.2)时,一切正常,没有任何错误,但每当我在远程服务器(IIS 7)上运行该应用程序时,我都会遇到这些错误和问题。

I'm working a web application with PHP.
Something wrong is happening that I have never seen before. $_GET is working well, but $_POST does not work exactly. Imagine the form below:

<form action="process.php" method="post">
   <input type="text" name="title" />
   <input type="submit" value="send" />
</form>

As you see, I've used post for method attribute of the form. In this case, the code below will return error:

<?php
$sentData = $_POST['title'];
echo($sentData);    
?>

Error message:

PHP Notice:  Undefined index: title in ...

But If I had used $_GET in php scripts and get in the html form codes, everything would work without any error.

There are something more strange.

  1. There are just one form that returns no error while I'm using POST, other forms return error.
  2. When I run this application locally (with Xampp - Apache 2.2) everything works fine without any error, but whenever I run the application an the remote server (IIS 7), I get these errors and problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长途伴 2024-12-27 16:20:43

所以我会用几种不同的方式来解决这个问题:

  1. @Dynamicus是正确的,这只是一个“通知”而不是致命错误(至少你在问题中没有这么说),所以这可能是一个差异您的 .ini 配置或使用配置定义来抑制像“error_reporting(0);”这样的错误
  2. 您可能需要在本地和服务器上备份当前正在运行的 .ini,然后将工作文件复制到您的服务器上,然后重新启动 Apache,看看这是否会产生影响。
  3. 为什么索引数组(即 $_POST)产生警告有点奇怪,但是执行“print_r($_POST);”或“var_dump($_POST);”查看内容或是否有任何问题。

如果您正在使用框架或进行路由的东西,这可能是值得研究的事情。

我希望这会有所帮助,并且有更多错误报告设置[此处] http:// php.net/manual/en/function.error-reporting.php

so i would approach this in a couple of different ways:

  1. @Dynamicus is correct, this is only a 'Notice' and not a fatal error (at least you didn't say so in the question), so this may be a difference in your .ini config or with a config definition to suppress errors like so 'error_reporting(0);'
  2. You may want to make a back up of your current running .ini on your local and on the server and copy the working one over to your server and restart Apache and see if that makes a difference.
  3. Why the indexed array (ie $_POST) is producing a warning is somewhat bizarre, but do a "print_r($_POST);" or a "var_dump($_POST);" to see the contents or if anything is off.

if you're using a framework or something that does routing, this could be something to look into.

i hope this helps and there are more settings for error reporting [here] http://php.net/manual/en/function.error-reporting.php

孤独患者 2024-12-27 16:20:43

您的输入元素需要名称和值属性。

Your input element needs both a name and value property.

绾颜 2024-12-27 16:20:43

最后我更改了我的文件所在的服务器。我在另一台服务器(相同的操作系统)上尝试了它们,一切正常。
我不明白原因是什么......这是我第一次遇到这个未知错误。
不过,还是谢谢大家的建议和意见。

Finally I changed the server that my files were located on it. I tried them on another server (same OS), and everything worked fine.
I could not get what was the reason... This was the first time that I was getting this unknown error.
However, thank you all for your suggestions and comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文