在表单提交后显示 PHP POST 结果,然后在单击页面“刷新”时重新加载相同的空白表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18676856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display PHP POST results after a form submit, then reload same blank form when page "Refresh" clicked
提问by Techron
After over 6 hours of searching here and other forums/blogs, still found no operational method to do this, all on same page; so I remain confident this has not been asked in exact same way: Enter some data to a form, submit, show results... then if user clicks "Refresh", show the original blank form and not show a browser message about "You are resending data, etc. etc." Here is the base code, it functions as expected, just desire to have starting blank form show after clicking browser "Refresh". I have attempted both PRG and Sessions methods without success.
在这里和其他论坛/博客搜索了 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>
采纳答案by makmonty
This solution uses the session. First stores in the session the post field if it exists and then redirects to the same page. If it finds the field in the session, it gets it and remove it from session and show it on the page.
此解决方案使用会话。首先在会话中存储 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>
回答by Moeed Farooqui
You can't just delete the $_POST
data from the server. The browser alerts it because it is stored by the browser. If it resubmits the data then it will send it back to the server and repopulate $_POST
您不能只是$_POST
从服务器中删除数据。浏览器会提醒它,因为它是由浏览器存储的。如果它重新提交数据,那么它会将其发送回服务器并重新填充$_POST
You can achieve this by setting a cookie / session
variable, which tells you the form was already processed.
您可以通过设置一个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>
Dont forget to empty the action as you have mentioned in question(bold) All on same page
不要忘记清空您在问题中提到的操作(粗体)全部在同一页面上
<form method="post" action="RfrshTst.php" >
^--Here^
回答by Sarim
Try this code. You need to use JS to refresh without POSTing again.
试试这个代码。您需要使用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>
回答by vladkras
even Wiki has an articlefor you. I wonder how couldn't you find it?
甚至 Wiki 也有一篇文章供您参考。我想知道你怎么找不到?
you can do it with php:
你可以用 php 做到:
<?php
// handle $_POST here
header('Location:yourscript.php');
die();
?>
JS:
JS:
window.location = window.location.href;
or Post/Redirect/Getwhich is the best I think
或发布/重定向/获取这是我认为最好的