[英]Display PHP POST results after a form submit, then reload same blank form when page “Refresh” clicked
经过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>
此解决方案使用会话。 首先将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>
试试这个代码。 您需要使用JS刷新而无需再次发布。
<!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>
甚至Wiki都有适合您的文章 。 我不知道你怎么找不到它?
您可以使用php来做到这一点:
<?php
// handle $_POST here
header('Location:yourscript.php');
die();
?>
JS:
window.location = window.location.href;
或Post / Redirect / Get这是我认为最好的
您不能只是从服务器删除$_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^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.