【问题标题】:Issues with POST method in PHPPHP中POST方法的问题
【发布时间】:2016-05-29 05:52:51
【问题描述】:

问题:

表单提交后未定义的 POST 变量。

研究和故障排除完成:

  • 在这里阅读大量问题,几乎所有问题都与表单字段上没有名称标签有关。我的所有字段都有标签和 ID。
  • 将我的 PHP.ini 配置为将 $HTTP_RAW_POST_DATA 设置为 -1
  • PHP.net 和 W3Schools 的后续教程

此时我迷路了。数据只是拒绝发布,一切都未定义。下面是显示问题的 HTML、PHP 和两个屏幕截图。

我在 Windows 上使用 PHPStorm 的内置服务器。

signup.html

<div class="text-center col-md-4 col-md-offset-4">
        <form id="user_signup" class="form-horizontal signInFields" action="../php/register.php" method="POST">
            <input type="text" id="first_name" name="first_name" placeholder="First Name">
            <input type="text" id="last_name" name="last_name" placeholder="Last Name">
            <input type="email" id="user_email" name="user_email" placeholder="Email">
            <input type="text" id="user_id" name="user_id" placeholder="User ID">
            <input type="password" id="user_password" name="user_password" placeholder="Password">
            <input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
            <button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
        </form>

register.php

// Variables from the sign-up form POST action
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$user_email = $_POST["user_email"];
$user_id = $_POST["user_id"];
$user_password = $_POST["user_password"];
$confirm_password = $_POST["confirm_password"];

// Add a new user to the database
$conn = new mysqli($servername, $username, $password);
$testQuery = mysql_insert($first_name,$last_name,$user_email,$user_id,$user_password);

if($conn->query($testQuery) === TRUE){
    echo "New Record Created Successfully!";
} else {
    echo "Error: " . $testQuery . "<br>" . $conn->error;
}

$conn->close();

填写了字段的表单

提交后的输出:

在这一点上,我感到困惑。据我从 W3Schools、PHP.net 和这里的各种问题中得知,我已经正确设置了它。然而,事情显然是不对的。任何和所有的帮助将不胜感激。

【问题讨论】:

标签: php html forms phpstorm


【解决方案1】:

编辑:

您正在使用 PHPStormbuilt-in web server,它现在存在一些问题,尤其是 POST 请求,例如WEB-17317。这方面也可以参考这个answer

这可能是您无法处理表单的原因。

因此,最好的解决方案是将您的 PHPStorm 配置为在您当前的项目中使用本地 Web 服务器(在您的情况下为 XAMPP)。请按照以下步骤进行操作:

  1. 将本地服务器的虚拟主机配置为当前项目目录为根目录
  2. 确保您在上述步骤中配置后可以访问项目文件。例如:http://localhost/signup.html 应该可以通过浏览器访问。
  3. 现在,在 PHPStorm 中,转到 Run -> Edit Configuration 并选择每个文件并配置每个文件的 url,如下图所示。例如。对于 signup.html 文件,使用 http://localhost/signup.html
    • 您不需要为项目中的所有文件设置 url(步骤 3)。只需对 default(起始)文件执行此操作 - 例如index.html 或 login.html 或 signup.html。表单中与&lt;a&gt;action="register.php" 链接的所有其他文件将自动由本地服务器提供。

现在,您可以使用 PHPStorm 中的 绿色 按钮来运行您的 HTML/PHP 文件,它会在您的浏览器中使用您的 url 打开这些文件在步骤 1 中配置。您应该能够毫无问题地处理您的表单。


但如果您的问题是由其他原因引起的,请考虑检查以下几点:

  • 表单字段应具有 name 属性,例如signup.html 文件中的“name='first_name”`。
  • echo $_SERVER["REQUEST_METHOD"] == "POST"register.php 文件中写入 1 时打印,这意味着您的表单已成功提交。
  • php.ini 中的值post_max_size 设置为#M 格式,例如10M,任何其他格式如10MB 都是无效的。参考php.net
  • 检查echo file_get_contents("php://input"); 是否以first_name=John&amp;last_name=Doe .... 格式显示数据。请参考php.net
  • 检查var_dump($_POST) 是否在数组中显示表单数据。

希望对您有所帮助。欢迎发表评论。

【讨论】:

  • 不幸的是,用上面的方法切换&lt;button id="btn_signup" type="submit" name="signup_button"&gt;Sign Me Up!&lt;/button&gt;不起作用=(
  • 它确实输出了 1。var_dump($_POST) 仍然返回一个空数组。即使有上面​​和下面的解决方案。
  • 只是引导程序。许多关于缺少名称标签导致此问题的帖子都在使用它,他们都说一旦添加了名称标签,它就可以工作。我也会检查这方面的。现在我正在研究为什么一个成功的 POST 实际上可能不包含任何数据。它提出了一堆 AJAX、JQUERY 和 PHP 解决方案。我可以尝试其中一种,例如提到的@user285743。
【解决方案2】:

试试这个

register.php

<?php

$errors = [];

$fields =
[
    'first_name',
    'last_name',
    'user_email',
    'user_id',
    'user_password',
    'confirm_password',
];

$data = [];


if( !empty( $_POST ) )
{
    foreach ($fields as $field)
    {
        if( isset( $_POST[$field] ) )
        {
            $data[$field] = $_POST[$field];
        }
        else
        {
            //empty field handling
            $errors[$field] = $field.' required!';
        }
    }
}

if( empty($errors) )
{
    // Add a new user to the database
    $conn = new mysqli($servername, $username, $password);

    $testQuery = mysql_insert
    (
        $data['first_name'],
        $data['last_name'],
        $data['user_email'],
        $data['user_id'],
        $data['user_password']
    );

    if($conn->query($testQuery) === TRUE)
    {
        echo "New Record Created Successfully!";
    }
    else
    {
        echo "Error: " . $testQuery . "<br>" . $conn->error;
    }

    $conn->close();
}

signup.php

<?php require('path/to/register.php'); ?>

  <form id="user_signup" class="form-horizontal signInFields" action="" method="post">
            <input type="text" id="first_name" name="first_name" placeholder="First Name">
            <input type="text" id="last_name" name="last_name" placeholder="Last Name">
            <input type="email" id="user_email" name="user_email" placeholder="Email">
            <input type="text" id="user_id" name="user_id" placeholder="User ID">
            <input type="password" id="user_password" name="user_password" placeholder="Password">
            <input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
            <button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
        </form>

旁注(如果您是新手)

  • 使用 method="post" 而不是 method="POST"(只是标准)

  • 您没有验证输入字段(危险)

【讨论】:

  • 不幸的是,这产生了相同的结果。似乎$_SERVER('REQUEST_METHOD') 以 POST 的形式返回。然而,实际数据并没有正确地过来。我现在正在调查。
【解决方案3】:

您可以使用输入类型按钮代替您正在使用的按钮,在这种情况下您可以使用 ajax 发送数据。

【讨论】:

  • 这是一个替代方案,而不是问题的解决方案
【解决方案4】:

我复制了您的表格和register.php 文件的(部分),没有任何问题。数据按预期发布。我之前也遇到过类似的事情,结果发现这是一个奇怪的重定向问题,所以当我的 POST 数据到达我的控制器时,它已经丢失了。遗憾的是,我只能提供一些调试建议。

我确实想提一下,一旦你弄清楚你的 POST 问题,你将需要查看你的数据库交互内容......你创建了一个 mysqli 对象,但调用了一个名为 mysql_insert 的函数(我的函数'从未见过,也无法在 php.net 上找到,所以我倾向于认为这不会做任何事情)。快速搜索将引导您找到许多关于如何使用 mysqli 的详尽教程和演练,因此我将遵循这些内容,而不是试图在这里解释所有内容(因为无论如何您仍在尝试解决 POST 问题)。

1] 您不必使用 PDO; mysqli 没问题,只要你正确使用它是完全可以接受的(here's some information about both from php.net directly)。

2] 我从未见过在&lt;form&gt; 标记中指定使用小写“post”而不是“POST”的标准,但我希望有一个指向它的链接!

3] &lt;input type="submit" value="text"&gt;&lt;button type="submit"&gt;text&lt;/button&gt; 在功能上 是一样的,两者都会发布表单没有问题。两者之间存在一些差异(例如,能够为 text 动态设置“值”而不是“innerHTML”,但这是另一个问题。

至于进一步的调试工作......在register.php 中,就像Orions 提到的那样,我很好奇$_SERVER['REQUEST_METHOD'] 报告了什么。在该文件的开头,尝试die($_SERVER['REQUEST_METHOD']) 并查看它的内容。如果它说“POST”,那么你就在正确的轨道上。如果您在该文件的开头执行var_dump($_POST);,然后执行死亡/退出,您会看到什么?


据我记得,有 2 个实例的重定向使我失去了我的 POST 变量,这两个都是由 mod_rewrites 引起的。一个是将请求从mydomain.com 重定向到www.mydomain.com,由于我的表单中的action 明确为mydomain.com,所以一切都丢失了。另一个问题是 mod_rewrite 从请求中附加或删除尾随 /(我不记得是哪个),但这也有相同的基本结果。如果您有 .htaccess 文件,请检查以确保没有任何恶意或陷阱发生。

其他需要检查的事项:

a] 在您的 php.ini 中,确保 variables_order 在某处有“P” (reference)

b] 同样在您的 php.ini 中,确保 post_max_size 是合理的;我怀疑这是问题所在,因为您没有上传文件,但如果将其设置为非常小的东西,我可以想象这可能是一个问题

c] 如果您从 http 页面发布到 https 页面,有时会删除 POST 数据(这可能是特定于服务器的,因为我觉得我有时会看到这项工作,而不是其他)

d] 您是否收到了其他地方建议的file_get_contents("php://input");?它是否包含您的数据(看起来像这样:key1=value1&amp;key2=value2)还是太空白?

e] 如果您在另一个页面上创建另一个表单并将其 POST 到另一个文件,是否会填充 $_POST 数组?

【讨论】:

  • $_SERVER['REQUEST_METHOD'] 报告说它确实是一个 POST 方法。 var_dum($_POST) 返回一个空数组。我很好奇它是否与您的重定向问题相似。那是怎么回事?
  • @Saphiric:请参阅我编辑的答案以获取有关我的特定重定向问题和其他一些尝试调试的信息。
猜你喜欢
  • 2015-01-11
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多