【问题标题】:Display PHP POST results after a form submit, then reload same blank form when page "Refresh" clicked表单提交后显示 PHP POST 结果,然后在单击页面“刷新”时重新加载相同的空白表单
【发布时间】:2013-09-07 19:07:39
【问题描述】:

在这里和其他论坛/博客搜索了 6 个多小时后,仍然没有找到操作方法,都在同一页面上;所以我仍然相信这并没有以完全相同的方式被问到:在表单中输入一些数据,提交,显示结果......然后如果用户点击“刷新”,显示原始的空白表单而不显示关于“你正在重新发送数据等。”这是基本代码,它按预期运行,只是希望在单击浏览器“刷新”后开始显示空白表单。我尝试了 PRG 和 Sessions 方法都没有成功。

<!DOCTYPE html >
<head>
<title>Refresher test</title>
</head>
<body>
<br/><br/><h2>What Me Refresh</h2>

<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])||(($_POST['text']) == "")){
?>  

<p><h3>Enter text in the box then select "Go":</h3></p>

<form method="post" action="RfrshTst.php" >
<textarea rows="5" cols="50" name="text" >
</textarea>
<input type="submit" name="submit" value="Go" />
</form>

<?php 
//If form submitted, process input.
} else {
//Retrieve show string from form submission.
$txt = $_POST['text'];
echo "The text you entered was : $txt";

} ?> 
</body>
</html>

【问题讨论】:

  • 你确定这是指 php 而不是常见的浏览器行为?
  • 在表单上重新加载将重新提交帖子变量。页面刷新需要js设置隐藏字段

标签: php forms


【解决方案1】:

此解决方案使用会话。 首先在会话中存储 post 字段(如果存在),然后重定向到同一页面。 如果它在会话中找到该字段,它会获取它并将其从会话中删除并显示在页面上。

<?php

$txt = "";
session_start();

if (isset($_POST['submit']) && (($_POST['text']) != "")) {
    $_SESSION['text'] = $_POST['text'];
    header("Location: ". $_SERVER['REQUEST_URI']);
    exit;
} else {
    if(isset($_SESSION['text'])) {
        //Retrieve show string from form submission.
        $txt = $_SESSION['text'];
        unset($_SESSION['text']);
    }
}

?>

<!DOCTYPE html >
<head>
<title>Refresher test</title>
</head>
<body>
<br/><br/><h2>What Me Refresh</h2>

<?php
if($txt != "") {
    echo "The text you entered was : $txt";
} else {
?>


<p><h3>Enter text in the box then select "Go":</h3></p>

<form method="post">
<textarea rows="5" cols="50" name="text" >
</textarea>
<input type="submit" name="submit" value="Go" />
</form>

<?php } ?>

</body>
</html>

【讨论】:

  • 这段代码完美地实现了该功能,并且正是我正在寻找的添加,我尝试过类似的,但我的标题代码出错了。谢谢一堆!
【解决方案2】:

试试这个代码。需要使用JS刷新,无需再次POST。

<!DOCTYPE html >
<head>
<title>Refresher test</title>
</head>
<body>
<br/><br/><h2>What Me Refresh</h2>

<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])||(($_POST['text']) == "")){
?>  

<p><h3>Enter text in the box then select "Go":</h3></p>

<form method="post" action="RfrshTst.php" >
<textarea rows="5" cols="50" name="text" >
</textarea>
<input type="submit" name="submit" value="Go" />
</form>

<?php 
//If form submitted, process input.
} else {
//Retrieve show string from form submission.
$txt = $_POST['text'];
echo "The text you entered was : $txt";

?>

<button onclick="location = location.href">Refresh</button>
<?php

} ?> 
</body>
</html>

【讨论】:

  • 这是一个很好的解决方案,但我希望使用浏览器的“刷新”按钮,有什么想法吗?
  • 试试这个方法 > GET form > POST ,将提交的文本保存在 cookie 或会话中。重定向到 form ,显示来自 cookie 的文本。
【解决方案3】:

即使 Wiki 也为您提供 an article。我想知道你怎么找不到它?

你可以用php做到这一点:

<?php
// handle $_POST here
header('Location:yourscript.php');
die();
?>

JS:

window.location = window.location.href;

Post/Redirect/Get这是我认为最好的

【讨论】:

  • 是的,我已经看到了,PRG 方法似乎需要不同的页面。
【解决方案4】:

您不能只从服务器中删除$_POST 数据。浏览器会提醒它,因为它是由浏览器存储的。如果它重新提交数据,它会将其发送回服务器并重新填充$_POST

您可以通过设置cookie / session 变量来实现此目的,该变量告诉您​​表单已被处理。

<?php session_start(); ?>
<!DOCTYPE html >
<head>
<title>Refresher test</title>
</head>
<body>
<br/><br/><h2>What Me Refresh</h2>

<?php
//If form not submitted, display form.
if (isset($_POST['submit']) && !isset($_SESSION['user'])){

//Retrieve show string from form submission.
$txt = $_POST['text'];
echo "The text you entered was : $txt";

$_SESSION['user'] = true;
//If form submitted, process input.
} else {
?>
<p><h3>Enter text in the box then select "Go":</h3></p>

<form method="post" action="" >
<textarea rows="5" cols="50" name="text" >
</textarea>
<input type="submit" name="submit" value="Go" />
</form>

<?php
} ?> 
</body>
</html>

不要忘记清空您在问题中提到的操作(粗体)全部在同一页面上

<form method="post" action="RfrshTst.php" >
                              ^--Here^

【讨论】: