Jump to content


Photo

refreshing webbrowser re-inserts data in database


  • Please log in to reply
4 replies to this topic

#1 Snip

Snip
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 01 March 2006 - 03:51 PM

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. 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.


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?

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 March 2006 - 04:02 PM

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.

follow me on twitter @PHPsycho

#3 Snip

Snip
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 01 March 2006 - 04:17 PM

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.

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 March 2006 - 04:23 PM

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!
follow me on twitter @PHPsycho

#5 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 01 March 2006 - 04:37 PM

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.
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users