1internet Posted January 16, 2013 Share Posted January 16, 2013 So if I have multiple forms performing different functions on the same page, is it best to put all the php code at the top of the page before I declare the doctype? Oppossed to the php code above each form? Is this considered best practice? Is this also what prevents the header already assigned errors that need to be corrected with the ob_start, etc functions? Quote Link to comment https://forums.phpfreaks.com/topic/273254-good-practice-with-multiple-forms-on-a-page/ Share on other sites More sharing options...
RussellReal Posted January 16, 2013 Share Posted January 16, 2013 Well, I'll be quite fair, if you're going to be submitting multiple forms or just 1 form, you're best running the processing code in a separate file "process.php" or similar. The reason I say this, is because if you use the same page as the recipient, its very hard for you to avoid accidental form resubmission (hitting the refresh button) You could also use the same page as the processing page (If you absolutely MUST) aslong as you redirect the user to a "confirmation" page, and get them away from that page AFAP (sounds dirty huh!) But to answer your question, yes -- running your form processing code before ANY character data (even spaces and new lines) will avoid the "headers already sent" error, but if you echo, print, etc, information to the browser before you call header() you will receive that error anyway. - Russell Quote Link to comment https://forums.phpfreaks.com/topic/273254-good-practice-with-multiple-forms-on-a-page/#findComment-1406244 Share on other sites More sharing options...
1internet Posted January 16, 2013 Author Share Posted January 16, 2013 Thanks Russel, what if I don't use the same page as the recipient, or when I do, the form is no longer shown? Quote Link to comment https://forums.phpfreaks.com/topic/273254-good-practice-with-multiple-forms-on-a-page/#findComment-1406245 Share on other sites More sharing options...
RussellReal Posted January 16, 2013 Share Posted January 16, 2013 Nope, usually on Error you will forward the user back to the page with the form like: header('Location: index.php?err=1'); Quote Link to comment https://forums.phpfreaks.com/topic/273254-good-practice-with-multiple-forms-on-a-page/#findComment-1406247 Share on other sites More sharing options...
Jessica Posted January 16, 2013 Share Posted January 16, 2013 It's not hard to avoid the refresh problem - when done processing, use header to send them back to the same page. Just like if you use a separate page. ... Quote Link to comment https://forums.phpfreaks.com/topic/273254-good-practice-with-multiple-forms-on-a-page/#findComment-1406248 Share on other sites More sharing options...
RussellReal Posted January 16, 2013 Share Posted January 16, 2013 Yes, but the only reason people will use the same page to process form data is if they're trying to use the form data collected in targeting erroneous fields. (For example: Long Profile Detail Pages -- which should use AJAX of some sort to be quite fair) While that is a decent approach, for that application, in almost every other application its best to process on separate pages, atleast in my professional experience. - Russell Quote Link to comment https://forums.phpfreaks.com/topic/273254-good-practice-with-multiple-forms-on-a-page/#findComment-1406250 Share on other sites More sharing options...
1internet Posted January 16, 2013 Author Share Posted January 16, 2013 I currently dont use a redirect for the errors, if there are no errors it redirects to another page, but if there errors it will remain on the same page from the form action="", with the $errors loaded in the array and displayed to the user. Is this approach acceptable? Quote Link to comment https://forums.phpfreaks.com/topic/273254-good-practice-with-multiple-forms-on-a-page/#findComment-1406252 Share on other sites More sharing options...
RussellReal Posted January 16, 2013 Share Posted January 16, 2013 Yes, that approach is what Jessica outlined in her post and I outlined in my follow-up post. - Russell Quote Link to comment https://forums.phpfreaks.com/topic/273254-good-practice-with-multiple-forms-on-a-page/#findComment-1406256 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.