【问题标题】:$_POST empty on submit$_POST 在提交时为空
【发布时间】:2014-05-19 14:13:52
【问题描述】:

我有一个注册脚本(称为“script.php”),分为 3 个步骤;这是基本结构(我已经去掉了一些东西,比如清理用户输入并防止直接访问除 1 之外的其他步骤):

<?php
$step = $_GET['step'];

switch($step) {
    case 1:
        //show form - action="script.php?step=2" method="post"
        break;
    case 2:
        //if user data is good show an overview of the data and show a button to go to step 3 (the button is enclosed in a form - action="script.php?step=3" and method="post")
        //if not, show again form - action="script.php?step=2" method="post" - and display errors
        break;
    case 3:
        //add user data into db
        break;
}
?>

真实代码:

<?php
switch ($step) {
case 1:
    //display form
    $html = <<<FORM
        <form action="/install?step=2" method="post">

            <input type="text" name="username">
            <input type="email" name="email">
            <input type="password" name="password">
            <input type="password" name="password_repeat">
            <button class="next" type="submit">Next</button>
        </form>
FORM;
    break;
case 2:
    $errors = array();
    if (empty($_POST['username'])||!preg_match_all('/^([A-Za-z0-9]+){1,16}$/', $_POST['username'])) {
        $errors[] = 'bad/empty username';
    }
    if (empty($_POST['email'])||!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'bad/empty email';
    }
    if (empty($_POST['password'])) {
        $errors[] = 'empty password';
    }
    if (empty($_POST['password_repeat'])) {
        $errors[] = 'empty password confirm';
    }
    if ((!empty($_POST['password'])&&!empty($_POST['password_repeat']))&&$_POST['password']!==$_POST['password_repeat']) {
        $errors[] = 'passwords do not match';
    }

    if(count($errors)>0) {
        $error_html = 'some errors occurred';
        foreach ($errors as $err) {
            $error_html .= 'error: '.$err;
        }
        $form_html = <<<FORM
            <form action="/install?step=2" method="post">

                <input type="text" name="username" value="{$_POST['username']}">
                <input type="email" name="email" id="email" value="{$_POST['email']}">
                <input type="password" name="password">
                <input type="password" name="password_repeat">
                <button class="next" type="submit">Next</button>
            </form>
FORM;
        $html = $error_html.$form_html;
    }
    else {
        $ent = 'htmlentities';
        $html = <<<DATA_OK
            <h3>Data overview</h3>
            <table>
                <tr>
                    <th>Username:</th>
                    <td>{$ent($_POST['username'])}</td>
                </tr>
                <tr>
                    <th>email:</th>
                    <td>{$ent($_POST['email'])}</td>
                </tr>
                <tr>
                    <th>Password:</th>
                    <td>{$ent($_POST['password'])}</td>
                </tr>
            </table>

            <form action="/install?step=3" method="post">
                <button class="next" type="submit">Avanti</button>
            </form>
DATA_OK;
    }
    break;
case 3:
    //connect to db and insert data
    break;
}
?>
<!doctype HTML>
<html>
<head>
    <title>Script</title>
    <meta charset="utf-8">
</head>
<body>
    <?php echo $html; ?>
</body>
</html>

问题是当我进入第 3 步时,$_POST 总是空的。步骤 2 中显示的按钮(如果用户数据良好)是否覆盖了 $_POST?还是因为该表单没有输入而只有提交而被清空?如何在不使用隐藏字段的情况下将 $_POST 数据传递到第 3 步(因为它们将包含密码/pw 哈希)?

我已经在谷歌和这里搜索过,但我找不到与我的问题相关的任何内容。

【问题讨论】:

  • 你为什么要从$_GET中提取你的价值?
  • 请张贴表单 HTML。你的表单有 method="post" 吗?
  • @Joe 是的,我正在使用休息时间。我将它们添加到代码中,我认为它们无关紧要。
  • action="script.php?step=3" 的表单是否包含所有表单字段?数据概览与实际(隐藏...)字段不同。
  • @pinetreesarecool 最好将整个真正的 switch 语句添加到真正的 html 输出等中,混合 get 和 post 没有这样的已知问题,所以它可能在你的代码的其余部分是具体的

标签: php forms post


【解决方案1】:
<form action="/install?step=3" method="post">
   <button class="next" type="submit">Avanti</button>
</form>

您希望在第 3 步中发布哪些价值观?你的表格里没有。甚至按钮也没有name 属性。具有 name 属性的表单中的任何字段都不会发送 EMPTY 帖子。

POST 数据的处理方式与隐藏字段相同。发帖的人可以看到他发的内容,所以没有理由对他隐瞒。如果他从步骤 1 中发布了密码,他就知道他发布了什么。您可以对其进行哈希处理,并将其设置为会话/隐藏字段,即使认为这是不好的做法。

我真的不明白为什么你有两个步骤。如果第 2 步只有一个按钮,为什么会有它?可以在第1步进行验证,如果可以,则进入第3步,如果不行,就留下来。

【讨论】:

  • 所以这个表单正如我所想的那样覆盖了我的 $_POST。通过步骤 2 中的数据检查后如何传递数组?在那个阶段它不应该是空的,不是吗?
  • @pinetreesarecool 序列化、会话或隐藏字段。数据不会留在内存中。这不像桌面应用程序在打开时一直使用您的内存。对新页面或同一页面的每个请求都会清除内存,就像您关闭应用程序并再次打开它一样。我扩展了我的答案。
  • 感谢您的回答。我有两个(实际上是三个)步骤,因为流程应该是:第 1 步:用户输入 --> 第 2 步:输入很好,显示一个概览。如果用户想继续 --> 步骤 3:将数据添加到 db
猜你喜欢
  • 2013-06-04
  • 1970-01-01
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
相关资源
最近更新 更多