Snip Posted March 1, 2006 Share Posted March 1, 2006 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? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 1, 2006 Share Posted March 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
Snip Posted March 1, 2006 Author Share Posted March 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 1, 2006 Share Posted March 1, 2006 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! Quote Link to comment Share on other sites More sharing options...
XenoPhage Posted March 1, 2006 Share Posted March 1, 2006 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 SubmitPage 2 - Handle the data and forward to page 3Page 3 - Display the data how I want itIf 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.