doubledee Posted September 3, 2011 Share Posted September 3, 2011 Can someone give me some guidance of how to separate Form Display from Form Processing? I have always used forms that submitted back to themselves which isn't so bad, but then trying to cram in code to display the form, validation errors, and messages after the form is processed all in one file is insane?! Currently I am working on a simple "Add a Comment" form. It would be nice to have a separate form processing script, but I don't know where to begin... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/ Share on other sites More sharing options...
sunfighter Posted September 3, 2011 Share Posted September 3, 2011 I place my js validating section in the head of the page that holds the form just to make sure errors and omissions are caught before the form is processed. In the <form action="PUT FORM PROCESSING FILE NAME HERE" ... Do the same thing in this file as you were did in the one page form. Get your form variables and validate and process. Then go to next web page. Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/#findComment-1265117 Share on other sites More sharing options...
Psycho Posted September 3, 2011 Share Posted September 3, 2011 I typically have one page, but that page will include() the code necessary to either validate and/or display the form Rough example on the "main" page (e.g. 'myform.php') //Var to hold validation error (if form was posted) $errors = array(); if(isset($_POST['someformfield'])) { //Include the file that does the form validation include('form_processing_page.php'); //If any validation errors occur, they will be added to the $errors array //If validation passes the validation page can process/save the data OR call another page to do that logic //But, at the end of processing, user is redirected to a confirmation page using header() followed by exit() //so the remaining code on this page is not executed } //Include the html form page // This only happens if the form was not submitted or if there were validation errors include('form_html.php'); //The html content page will include logic to display the errors if the exist Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/#findComment-1265198 Share on other sites More sharing options...
doubledee Posted September 4, 2011 Author Share Posted September 4, 2011 I typically have one page, but that page will include() the code necessary to either validate and/or display the form Rough example on the "main" page (e.g. 'myform.php') //Var to hold validation error (if form was posted) $errors = array(); if(isset($_POST['someformfield'])) { //Include the file that does the form validation include('form_processing_page.php'); //If any validation errors occur, they will be added to the $errors array //If validation passes the validation page can process/save the data OR call another page to do that logic //But, at the end of processing, user is redirected to a confirmation page using header() followed by exit() //so the remaining code on this page is not executed } //Include the html form page // This only happens if the form was not submitted or if there were validation errors include('form_html.php'); //The html content page will include logic to display the errors if the exist That is basically what I do now, except for another script printing out the results. My big problem - and I don't see how your approach is immune - is if the user hits Back/Forward/Back/Forward then you get into Double-Submission issues... :-\ Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/#findComment-1265244 Share on other sites More sharing options...
KevinM1 Posted September 4, 2011 Share Posted September 4, 2011 Do a google search on the PRG Pattern (I'd type more, but I'm on my PS3). Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/#findComment-1265264 Share on other sites More sharing options...
doubledee Posted September 4, 2011 Author Share Posted September 4, 2011 Do a google search on the PRG Pattern (I'd type more, but I'm on my PS3). From what I've read, it is a poor pattern... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/#findComment-1265270 Share on other sites More sharing options...
KevinM1 Posted September 4, 2011 Share Posted September 4, 2011 How so? Its purpose is to stop double form submissions. Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/#findComment-1265273 Share on other sites More sharing options...
doubledee Posted September 4, 2011 Author Share Posted September 4, 2011 How so? Its purpose is to stop double form submissions. I don't have the link, but what I read last night said that that pattern wasn't such hot stuff and had flaws in it. It might have been on wiki? Don't remember. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/#findComment-1265275 Share on other sites More sharing options...
KevinM1 Posted September 4, 2011 Share Posted September 4, 2011 All patterns have strengths and weaknesses. That's why they're patterns and not silver bullets. Each pattern is a common, tested, working approach to solve a particular problem. Your problem is double submissions. The PRG Pattern exists to solve that particular problem. Quote Link to comment https://forums.phpfreaks.com/topic/246324-separating-form-display-from-processing/#findComment-1265279 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.