0

The form gets submitted. But after that when I refresh it firefox gives this message "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier." and gives two options "Resend" or "Cancel".

It does not happen with the form with "GET" method.

My code for form.php is as follows.

<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
</body>
</html>

I guess the solution for this lies in "Redirect after POST" (php) or form.reset()...(javascript) or HTML

I have gone through many articles but I am unable to implement it as there is lot of theory available but no working example. I hope you guys and this site can help me. It would be the biggest achievement for me if I get success in it. Thank you!!

  • That's what happens when you refresh a POST request, it's totally normal ? – adeneo Sep 09 '17 at 20:12
  • The only way I know of avoiding this: use Ajax to do the post. – Keith Sep 09 '17 at 20:15
  • Redirect after POST is not necessarily the solution, because what you are seeing is totally normal under the circumstance you describe. One thing is for certain though, and that's that you should be using some sort of form token + session to make sure that when you do go back in history, that if you "resend" it does not double post. – Brian Gottier Sep 09 '17 at 20:16

7 Answers7

3

Yes, that is normal. A common practice is to let the page redirect to itself after form processing with the php header() function:

header("Location: http://www.example.com/");
Benni
  • 1,023
  • 11
  • 15
  • 1
    I noticed that someone down voted this answer, while it's actually is a solution to the OP's issue. If you down vote, the helpful and decent thing would be to actually leave a comment why. – M. Eriksson Sep 09 '17 at 20:23
1

This is normal browser behaviour. If you dont want this, simply redirect the user to a page after handling the POST data.

You can do this by the following code:

header('Location: redirectpage.php');

Note that this can be the very same page as you just came from, but after a redirect, the POST container will be empty, and thus your browser will not prompt you about resending data.

Erik Baars
  • 2,278
  • 1
  • 8
  • 14
  • I noticed that someone down voted this answer, while it's actually is a solution to the OP's issue. If you down vote, the helpful and decent thing would be to actually leave a comment why. – M. Eriksson Sep 09 '17 at 20:23
0

youll have to do something like: header("Location: http://www.example.com/"); or if you already sent headers you can do something like this:

//Complete response of sending form
<?php
echo "<meta http-equiv=\"refresh\" content=\"0; url=http://example.com/\"   />\n";
?>

Or try This:

<script>
location.href = "youroriginalpage.php";
</script>

Or do something like below:

 <?php
 if( $_POST )
{
   if( empty($_POST['file']) )
 {
   // echo error or do nothing;
   die("Select a file to upload");
     }
   }
 ?>
 <html>
 <body>
 <form action="" method="POST" enctype="multipart/form-data">
 <input type="file" name="image">
 <input type="submit" value="Submit">
 </form>
 </body>
 </html> 
0

After submitting a form with a “POST” method when I refresh it asks to resend the information

That is the correct behavior.

I guess the solution for this lies in "Redirect after POST" (php) or form.reset()...(javascript) or HTML

You must design your applications following the POST-Redirect-GET (PRG) pattern. Basically, you need PHP scripts that receive data from the POST forms. That scripts must redirect the browser to the same or another script using a GET request. Because the last script is retrieved using GET, if you refresh the page, you will not get the message neither the data will be submitted again.

// For an script that shows the form and receives the data

// detect if it is received by POST
if (count($_POST)) {

  // process the file using a function 
  process_upload($_FILES["image"]);

  // redirect the browser to the same page
  header("Location: ".$_SERVER['PHP_SELF']);
  exit();
}
<html>
<body>
  <form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="image">
    <input type="submit" value="Submit">
  </form>
</body>
</html>

For other examples, you may check:

Jaime
  • 5,435
  • 2
  • 18
  • 21
0

Try this:

<?php
if( $_POST )
{
   if( empty($_POST['file']) )
     {
       // echo error or do nothing;
       die("Select a file to upload");
     }
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
</body>
</html

Hope this helps!

Community
  • 1
  • 1
Prince Adeyemi
  • 724
  • 6
  • 12
0

This is a normal browser behavior. To avoid such thing, print out a redirect code such as:

<script>
location.href = "youroriginalpage.php";
</script>

You need to make your form submit to a page other than the one with the form, which is also a better approach in my opinion.

Another approach would also be to send an AJAX request instead which will keep the user on the same page.

Feras Wilson
  • 380
  • 1
  • 3
  • 13
  • I followed your suggestion and it worked like a charm!! However, I submit the form to the same page and redirected to the same page. Now, it looks very easy. Thank you guys. – Prakash Patil Sep 10 '17 at 04:01
0

PRG pattern can be rephrased like this:

  • Never show pages in response to POST
  • Always load pages using GET
  • Navigate from POST to GET using REDIRECT