【发布时间】: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 没有这样的已知问题,所以它可能在你的代码的其余部分是具体的