Jump to content


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


refreshing webbrowser re-inserts data in database

Recommended Posts

I have a php-webpage with a form on it.
When I submit the form I check the submitted data with a self-written php-validator-class. When the validator-class says that there are no errors I insert the data in the database using a self-written php-DBFactory-class and the datatable on the webpage is updated with the new data.
When the validator-class says that there are errors, the page does not write to the database, but instead showes the errors.

This all workes fine. [b]But my problem now is that when I submit the data and the data gets written to the database, and I than click refresh on the webbrowser, the data gets written a second time to the database and the datatable on the webpage also gets reloaded with the new data (the new data is inserted twice).

So the data from the POST is re-used when I refresh the webbrowser.[/b]

How can I solve this refresh-problem?
I heard about something like having a processing-step in between pages that makes sure that when you submit your form, a "new" page gets shown so that on refresh, the POST-data is not there anymore.
But I wouldn't know how to do this.

Does anybody have an idea?

Share this post

Link to post
Share on other sites
When all the checks have be done and the query is executed, have the next line of code...

header("Location: " . $url);

Now url shold be another script that will display whether the query was completed or not.

as part of url u can pass paramters so that this script can display messages and possible links like so...

$url = "message.php?mssg=succes&back=form.php&next=index.php";

you can then do anything with those parameters in the new script and if you hit refresh it won't execute teh query again.

Share this post

Link to post
Share on other sites
I don't want to see an other page, I want it to be the same page, with the updated data.

I had seen the "header"-solution on many webpages, but the warning I always get with that solution is
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: Cannot modify header information - headers already sent by ...[/quote]

So my guess is that there should be kind of a solution where you have your form-page, witch after validation, forwards to a "control-page of some sort" witch inserts the data and redirects to the page where you inserted the data, but with the new data and the same form showing.

Share this post

Link to post
Share on other sites
well if you use the header function it must be used before you output any html.

Now as you want to prevent this refresh problem, using the header to change the page after processing the data means that you no longer need to output any html in the script that does the insert/edit/delete form teh database. Therefore you won't get that error!

Share this post

Link to post
Share on other sites
Toon is right.. The way I handle these situations is to submit the form to a handler script. That script has one single purpose. Manipulate the data and do whatever is needed with the database. Based on the results from that, I use either a header statement, or a javascript redirect to jump to the page I want to actually display.

So my data flow is something like this :

Page 1 - Enter Data and click Submit
Page 2 - Handle the data and forward to page 3
Page 3 - Display the data how I want it

If you use header() or a javascript window.location.replace(), then you avoid the back button problem where the user hits back and the data is resubmitted again.

There's additional power here too.. You can redirect to an error page if there's something wrong with the input, redirect to a multitude of different pages based on the input, etc.

Share this post

Link to post
Share on other sites


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.