Display PHP POST results after submitting the form, and then reload the same blank form when the Refresh page is clicked

After more than 6 hours of searching here and other forums / blogs, no operational method was still found for this, all on one page ; so I remain sure that this was not asked in the same way: enter some data in the form, submit, show the results ... then if the user clicks "Refresh", show the original blank form and will not show the message in the browser "You send data and etc. etc. " Here is the basic code, it functions as expected, just the desire to start an empty form after clicking the Refresh browser. I tried using the 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> 
+4
source share
4 answers

This solution uses a session. First saves the post field in the session if it exists, and then redirects to the same page. If he finds a field in a session, he will receive it and remove it from the session and display 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> 
+2
source

Try this code. You need to use JS to upgrade 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> 
0
source

even a wiki has an article for you. I wonder how you did not find him?

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 I consider the best

0
source

You cannot just delete $_POST data from the server. The browser warns about this because it is stored in the browser. If he resubmits the data, he sends them back to the server and closes $_POST again

This can be done by setting the cookie / session variable, which indicates that the form has already been 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> 

Remember to clear the action, as you already mentioned (in bold) All on one page

 <form method="post" action="RfrshTst.php" > ^--Here^ 
0
source

Source: https://habr.com/ru/post/1501100/


All Articles