简体   繁体   English

提交表单后显示PHP POST结果,然后在单击“刷新”页面时重新加载相同的空白表单

[英]Display PHP POST results after a form submit, then reload same blank form when page “Refresh” clicked

After over 6 hours of searching here and other forums/blogs, still found no operational method to do this, all on same page ; 经过6个多小时的搜索,在这里和其他论坛/博客上,仍然找不到可操作的方法, 全部都在同一页面上 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. 我尝试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>

This solution uses the session. 此解决方案使用会话。 First stores in the session the post field if it exists and then redirects to the same page. 首先将post字段(如果存在)存储在会话中,然后重定向到同一页面。 If it finds the field in the session, it gets it and remove it from session and show it on the page. 如果在会话中找到该字段,它将获取该字段并将其从会话中删除并显示在页面上。

<?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>

Try this code. 试试这个代码。 You need to use JS to refresh without POSTing again. 您需要使用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>

even Wiki has an article for you. 甚至Wiki都有适合您的文章 I wonder how couldn't you find it? 我不知道你怎么找不到它?

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/Get which is the best I think Post / Redirect / Get这是我认为最好的

You can't just delete the $_POST data from the server. 您不能只是从服务器删除$_POST数据。 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

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^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM