HTML表格发布方法到PHP:$ _POST空

时间:2018-01-12 23:23:41

标签: php html post

我搜索了其他几个类似的问题,但没有一个能帮助我。

我正在处理一个使用PHP发布到数据库的表单,但是没有发送帖子数据。 $ _POST是一个空数组,file_get_contents(' php:// input')返回一个空字符串。

这是一个MWE。

HTML:

<!DOCTYPE html>
<html>
<head></head>
<body>   
<div class="content">
    <form action="formprocessor.php" method="post">
        <label>Name: </label>
        <input name="name" type="text" />

        <label>Email: </label>
        <input name="email" type="text" />

        <input name="mySubmit" type="submit" value="Submit!" />
    </form>
</div>
</body>
</html>

formprocessor.php:

<?php

ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

var_dump(file_get_contents('php://input')); //returns string(0) ""
//or
var_dump($_POST); //returns array(0) {}

echo $_POST;
?>

html和php位于同一目录中的单独文件中。提交此表单后,我在浏览器中看到了这个:

string(0) "" array(0) { } Array

在Windows Server 2008上运行PHP 5.3.28

我确信代码没有任何问题,但可能与服务器或php配置有关。任何方向都会有所帮助。

我检查了配置文件。我能够识别的唯一相关设置是post_max_size,其值为8M。

以下是提交表单后Chrome网络标签在Chrome的开发人员工具上显示的内容: enter image description here

1 个答案:

答案 0 :(得分:0)

感谢DarkBee指出我正确的方向,但我发现了问题。我有URL重写规则来掩盖.html和.php扩展名。这导致post动作从&#34; formprocessor.php&#34;重定向。 to&#34; formprocessor&#34;,此时的帖子数据似乎丢失了。

我将以下内容放在子目录中的web.config中以禁用规则,现在它按预期工作。

<system.webServer>
<rewrite>
  <rules>
    <remove name="Redirect .php extension" />
    <remove name="hide .php extension" />
  </rules>
</rewrite>

事后看来,这应该是非常明显的。如果有人知道一种更清晰的方法来避免这种冲突,除了禁用重写规则,这将是有帮助的。希望这可以帮助其他人。

相关问题