How do I organize my code when developing forms to meet the following requirements?
I'm simply trying to figure out the correct way to lay out the architecture for my forms to meet the following requirements:
- The form must have server side validation.
- If a user is filling out a form on domain.com/register and the form doesn't pass server side validation, they should be brought back to domain.com/register with the errors displaying.
- (continued from point #2) If the user is brought back to domain.com/reg开发者_运维百科ister to fix validation errors, the data they have already entered in to the inputs should automatically display.
- If a user clicks back or forward on the form page the browser should not throw the "Confirm Form Resubmission" warning.
I'm a bit confused on where the form submit should be posting to. Should the form on domain.com/register post to a different page that simply handles the validation? If so, how do I pass the validation errors and inputed data back to domain.com/register?
Or, should the form be posting to itself? If so, how do I prevent the browser from throwing the "Confirm Form Resubmission" warning when clicking the back or forward button?
A logical way of approaching this (that attempts to keep all related items as localised as possible), would be to use a single page that has a "mode" switch statement near the top with an "update" case in it that contains your validation code.
Within the form, you'd simply post to the same page that you're on, but you'd add a hidden field in the form called "mode" with a value of "update". As such when the form is submitted execution will flow into the "update" case of your switch statement and you can carry out the required validation.
If the validation succeeds, you'd usually kick forward to another page (via the use of header('Location: ...');
followed by exit();
) and if validation fails, execution will simply continue down the page at which point you should output the errors that occurred during the validation.
精彩评论