Jump to content

Double input of Form Data When Browser Is Refreshed


TecTao

Recommended Posts

This may be a question that I have the answer to being a mid-level PHP programmer but I don't.

 

I have a form page.  When submitted it posts to page2.  On page 2, it inserts variable into the database.  The queries the database and displays the information.

 

But....

 

If the viewer refreshes their browser, it now submits the information into the database again.

 

Not sure how to handle problem.

 

I think that if a session variable passed to page 2 in an if else statemen, it will keep this from happening when refreshed since the session variable is not passed in the refresh.  But I'm stuck.

I had this problem when first programming with PHP and form submission like you're doing.  What worked for me was to redirect the browser after submitting the data, then the data will not be submitted again if the user reloads the browser.

 

For instance:

 


session_start();

// Your input validation code...

// If data is valid, save it, then:

header("Location: http://...newlocation");
exit;

// Otherwise, chastise the user and ask them to correct it....  or not.

I just solved this one recently myself.  Post to yourself, process the post, and redirect with a header to a confirmation page.

 

This may be a question that I have the answer to being a mid-level PHP programmer but I don't.

 

I have a form page.  When submitted it posts to page2.  On page 2, it inserts variable into the database.  The queries the database and displays the information.

 

But....

 

If the viewer refreshes their browser, it now submits the information into the database again.

 

Not sure how to handle problem.

 

I think that if a session variable passed to page 2 in an if else statemen, it will keep this from happening when refreshed since the session variable is not passed in the refresh.  But I'm stuck.

i ran into this same problem recently. i tried everything in my power to delete the $_POST array. for example:

foreach($_POST as $key => $val){ unset($key); }

 

but nothing worked. i found out that $_POST is also handled by your browser, so it's much more difficult to get around. one way is the way you've stumbled upon. basically redirecting to a new page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.