重定向用户回到注册页面,但保留他们填写的表单在php


Redirect user back to registration page but keep their filled in form in php

我有一个registration.php页面和另一个registration_checker_page.php.

如果用户名和/或电子邮件在他们提交后,然后它会重定向到上一个(注册)页面。

但是他们填写的数据丢失了。有没有办法在不清除数据的情况下重定向他们?

if(mysql_num_rows($queryUser) > 0){
   echo '<p><div id="redirect"/></p>';
   }
function  redir {
code ... location.href = "registration.php";
}

编辑:填写的数据是名字,用户名,邮政编码等。但其他信息如密码等将被删除。

可以使用session或cookie

if(mysql_num_rows($queryUser) > 0){
echo '<p><div id="redirect"/></p>';
}
function  redir() {
$userName = $_POST['username'];
$email = $_POST['email'];
setcookie("userName", $userName, time()+3600);  /* expire in 1 hour */
setcookie("emial", $email, time()+3600);
code ... location.href = "registration.php";
}

在你的表单

<input type="text" name="userName"  value="<?php echo isset($_COOKIE['userName']) ? $_COOKIE['userName']:'' ?>">
<input type="text" name="email"  value="<?php echo isset($_COOKIE['email']) ? $_COOKIE['email']:'' ?>">

希望对大家有所帮助

在MNSHO[1]中,解决这个问题的最佳方法是确保这些值从一开始就不会丢失。

让我们假设,为了同样的讨论,在你的HTML中有一个名为"username"的字段

如果您正在使用GET(或常规重定向),则可以将username作为查询字符串,以便在注册页面上可用。

window.location.href = "registration.php?username=" . $_REQUEST(username);

然后,在您的注册页面:

<input name="username" type="text">

你要做的是看看你是否有一个已经存在的值。

<?php
$usernameValue = "";
if (isset($_REQUEST['username'])) {
  $usernameValue = $_REQUEST['username'];
}
// Typing on the fly, I may have the type of quotation marks
// flipped around.  N
echo '<input name="username" type="text" value="$usernameValue">';
// or this...
echo "<input name='username' type='text' value='$usernameValue'>";
?>

现在,如果你从另一个重定向到这个页面…

[1]

您可以将它们保存为get变量并将其附加到您引用它们的文件中,或者在服务器端执行此操作并通过临时ID保存它们。

第一个可能性:

location.href="registraion.php?e=thisdudes@ema.il&u=thisdude"

在文本框失去焦点后立即使用AJAX检查重复的用户名/电子邮件,并相应地通知用户。这就是现代网页的做法。

在检查重复的用户名/电子邮件之前等待用户填写完整的表单是否有原因?