4

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

4 回答 4

2

此解决方案使用会话。首先在会话中存储 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>
于 2013-09-07T19:25:16.913 回答
0

您不能只$_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^
于 2013-09-07T19:29:47.357 回答
0

试试这个代码。您需要使用 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>
于 2013-09-07T19:14:27.810 回答
0

甚至 Wiki 都有一篇文章给你。我想知道你怎么找不到?

你可以用php做到这一点:

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

JS:

window.location = window.location.href;

发布/重定向/获取这是我认为最好的

于 2013-09-07T19:15:37.193 回答