4

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.

<!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>
Techron
  • 55
  • 1
  • 1
  • 4

4 Answers4

2

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.

<?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>
makmonty
  • 465
  • 3
  • 12
  • 1
    This code allows the functionality perfectly and is exactly the addition I was looking for, I have tried similar, but I went wrong with the header code. Thanx a bunch! – Techron Sep 07 '13 at 21:11
0

Try this code. You need to use JS to refresh without POSTing again.

<!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>
Sarim
  • 3,124
  • 2
  • 20
  • 20
  • This is a good solution, but I desire to use the browser's "Refresh" button, any ideas? – Techron Sep 07 '13 at 19:31
  • try this approch > GET _form_ > POST , save submitted text in cookie or session. redirect to _form_ , show text from cookie. – Sarim Sep 07 '13 at 19:42
0

even Wiki has an article for you. I wonder how couldn't you find it?

you can do it with php:

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

JS:

window.location = window.location.href;

or Post/Redirect/Get which is the best I think

vladkras
  • 16,483
  • 4
  • 45
  • 55
0

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

You can achieve this by setting a cookie / session variable, which tells you the form was already processed.

<?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^
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23