在这里和其他论坛/博客搜索超过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>
答案 0 :(得分: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>
答案 1 :(得分: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>
答案 2 :(得分:0)
即使维基有an article给你。我想知道你怎么找不到它?
你可以用php:
来做<?php
// handle $_POST here
header('Location:yourscript.php');
die();
?>
JS:
window.location = window.location.href;
或Post/Redirect/Get这是我认为最好的
答案 3 :(得分: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^