Page 1 of 1

How does one avoid resubmitting search results?

Posted: Sun Jun 05, 2011 2:16 am
by drayarms
You must all be familiar with those annoying messages that pop up on your screen sometimes when you try to navigate a website backwards on you browser, like this one I get on Safari:

Are you sure you want to resend the form again? To reopen this page, Safari must resend a form. this might result in duplicate purchases, comments or other actions.

On google chrome, I get this:

Confirm Form Resubmission
This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed. Press Reload to resend that data and display this page.
I pro
So basically, I have a form which takes some inputs. Then on a second page, I process those inputs with a select query, and on the same page, display the results of the select query . Then on that display page, I have a few links which take the user to 3rd pages. When I try to hit the previous page button on one of the third pages to get back to the display page, I get those above messages and I'm required to refresh the page before my results are redisplayed. How can I avoid this. My suspicion is that this happens because i'm using the same php script to do both the processing and the displaying of the results. Any insights are appreciated.

If it helps, I'll include a simplified version of the page that processes the form and displays the results below.

Code: Select all

<?php

//address error handling

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);


//authenticate user
require('auth.php');






echo'			<div id="content" class=""> ';





						if (isset($_POST['submitted'])) {

							// Connect to the database.

        						require_once ('config.php');

							//Query the database.

							$sql = "SELECT * FROM members INNER JOIN images ON members.member_id = images.member_id WHERE members.ethnicity = '{$_POST['ethnicity']}' AND images.image_cartegory = 'main'  ";

 

							$query = mysql_query($sql);

 

							//Check for success here.

							if(!$query)

							{

    								die('<p>Could not retrieve the data because: <b>' . mysql_error(). '</p>');

    								// Handle as desired

							}else{  //If query is valid.

    								if(mysql_num_rows($query) > 0){

									
									
									//Retrieve each record.

       			 						while ($row = mysql_fetch_assoc($query)){ 

									//Define variables to be passed via url.

									$friend= $row['member_id'];

										

									//Print the records.


									echo"

									


									

								
			 						//Make sure an uploaded photo is present.  if ($row['image'] !== NULL) {

									echo"<a href='friend.php?friend=$friend' >";

 

                    	 						// Note that we are building our src string using the filename from the database.

                     	 						echo "<img src=\"images/" . $row['image'] . "\" width=\"140\" maxheight=\"154\" />";    

                   

                    	 						echo "</a>";   


			 						echo"<h2 style= 'position:relative;bottom:25px;'>" .$row['username']. "</h2>";


							
									//End of if there is an uploaded pic.} 
									

									
			 						}//End of while loop.



        		 

   		 						}else {//No rows returned. End of if number of rows is greater than 0 loop.

        
        								echo 'No results match this search query.' ;

    									}

							}//End of else if query is valid.

						}//End of if form is submitted statement.




echo'						

				

			

			</div> <!--closes content-->      ';








?>
 
 

Re: How does one avoid resubmitting search results?

Posted: Mon Jun 06, 2011 7:14 am
by adityamenon90
The point of going back to the "Display" page is to do the "SELECT query" once again and display the results, isn't it?

Well, you could make the processing happen, then store the results in a variable, and POST them to a new page which gets displayed without having a form submit dependency.

Re: How does one avoid resubmitting search results?

Posted: Mon Jun 06, 2011 2:45 pm
by thinsoldier
Are you sure you get that message when you hit back?

I know the messages you are talking about but in my experience if page 1 is a form then page 2 is the page you get after you submit the form, hitting back to go from 2 back to 1 should just show page 1's form without any warning messages.

Where I get the message is when I go:
A: Page 1 > Page 2 > Page 3 <back to Page 2
or
B: Page 1 > Page 2 > Refresh Page 2

You may think those warning messages are annoying now but I notice you sql is just selecting records. If your code was inserting a record every time the page was viewed or refreshed those warning messages would be seen as a blessing.

Anyway, the way I avoid it...
I don't see it as avoiding the message. I see it as avoiding the possibility of an accidental back-button or refresh-button click causing the same data to be entered twice, same product purchased twice, etc.
The way I avoid it is after I've done my database insertion I do a header(location:) redirect to another url to display the "thanks for submitting this form" message.

But in your case your POST data is used to display data on the page immediately following so the header() thing is useless for you.
In your case there's no reason I can see why you can't just use $_GET. If you used GET you wouldn't ever get that message. In fact I've heard people say "whenever you get/query/select something to show on a page, just use the GET method and only use POST when you are sending information that is going to be inserted into the database."

Re: How does one avoid resubmitting search results?

Posted: Thu Jun 09, 2011 9:18 am
by eskio
You can use session variable $_SESSION['submitted']=0 default i.e. page not submitted and assign 1 when the page is submitted.
When your query is valid you have to check first is the page is submitted or not

Code: Select all

if(!$query)
 {
          die('<p>Could not retrieve the data because: <b>' . mysql_error(). '</p>');
             // Handle as desired
 }else{  //If query is valid.
       if (0 == $_SESSION['submitted']){
            $_SESSION['submitted'] = 1;
            if(mysql_num_rows($query) > 0){  
               //Retrieve each record.
              while ($row = mysql_fetch_assoc($query)){ 
              ................
When you reload the page $_SESSION['submitted'] is already 1

Re: How does one avoid resubmitting search results?

Posted: Wed Dec 21, 2016 6:10 am
by kinjo
Here’s the mail I got recently when I get this problem again
It sounds like the issue is specific to the website you are using. - I would mention it to whoever maintains your work site.

It may be worth checking if you have any add-ons installed in IE or Chrome and try disabling them.
ERR_CACHE_MISS – Solutions Encyclopedia

Re: How does one avoid resubmitting search results?

Posted: Wed Dec 21, 2016 5:24 pm
by Christopher
Usually the way to avoid this form resubmission problem is to redirect to a success page when the form validates. Then the back button will not go back to the submitted form.

Re: How does one avoid resubmitting search results?

Posted: Fri Dec 23, 2016 6:04 pm
by Vegan
I see a lot of that myself, the cause is HTTP 500 type errors due to overloaded servers.

Best bet is to check the server to be sure its working good

Re: How does one avoid resubmitting search results?

Posted: Wed Jan 04, 2017 6:01 am
by thinsoldier
No. Listen to Christopher.