Form Handling Design
Posted: Sun Jun 15, 2003 10:14 am
I suspect that the answers here will be primarily based on personal preference, but how do you deal with form handling? I'm listing the approaches I've seen or used, if you have other patterns, please share them. I'm trying hard not to add my personal thoughts on these patterns in my opening post, but will add them later as a reply.
Single, Self-Submitting Page
I see this design fairly commonly in people's designs in the regular PHP forum section. You have a single page that handles the submission of lots of different forms and redisplays the appropriate page on success/failure.
The code tends to use either cascaded if's or a pair of switches:
Self-submitting webpages
A series of pages, each one self-submits, but on success redirects to the next page. Model code:
Forward Submitting Webpages
Like the above, but submit and validated on the next page, redirect back to previous page on failire.
"Interstitual" Pages
Seperate view pages from processing pages. A given viewed webpage submits to one or more processing pages. The processing page does all the validation and redirects to either successive or previous pages as needed.
Sample View Page:
Sample Processing Page:
Now obviously, I've left out a lot of details about how each method works, but I think you can see the outline and the steps involved in each. Do you have an obvious favorite, that you use every time? All of the methods can of course use functions to pull lots of the fluff out to a seperate file, for instance all the validation or the individual page displays, etc. All the methods could be OOP or not, so that's not an issue here. Are there other paradigms to be aware of?
Single, Self-Submitting Page
I see this design fairly commonly in people's designs in the regular PHP forum section. You have a single page that handles the submission of lots of different forms and redisplays the appropriate page on success/failure.
The code tends to use either cascaded if's or a pair of switches:
Code: Select all
if ("submit"==$_POST["submit"])
{
switch($_POST["stage"])
{
case "Step1" : // validate Step1's inputs + break
case "Step2" : // validate Step2's inputs + break
// ....
default : // handle an unknown stage -- hack attack
}
}
// the above section would set both a $curStage variable and any error/preload variables
switch($curStage)
{
case "Step1" : // display the first page of the site + break
case "Step2" : // display the second ...
// ...
default: // hanles an unknown stage
}A series of pages, each one self-submits, but on success redirects to the next page. Model code:
Code: Select all
if ("submit"==$_POST["submit"])
{
// validate inputs
if ($validated)
{
// update db, session, etc
header("Location: nextpage.php");
exit;
}
// error strings were set during the validate input phase is needed
}
// display the page, including error strings if presentLike the above, but submit and validated on the next page, redirect back to previous page on failire.
Code: Select all
if ("submit"==$_POST["submit"])
{
// validate inputs
if ($validated)
{
// update db, session, etc
$_SESSION["formVars"]=array();
$_SESSION["errors"]=array();
}
else
{
$_SESSION["formVars"]=$formVars;
$_SESSION["errors"]=$errors;
header("Location: prevPage.php");
exit;
}
}
// display page using $_SESSION["formVars"/"errors"] for preloads or messagesSeperate view pages from processing pages. A given viewed webpage submits to one or more processing pages. The processing page does all the validation and redirects to either successive or previous pages as needed.
Sample View Page:
Code: Select all
// display page using values in $_SESSION["formVars"/"errors"] as show above, for error handling or previously filled in values.Code: Select all
if ("submit"==$_POST["submit"])
{
// validate, save current values form values, set error messages if
// needed
}
else
{
$validated=FALSE;
}
if ($validated)
{
// handle DB updates, setup new session variables
header("Location: nextPage.php");
exit;
}
else
{
$_SESSION["formVars"]=$formVars;
$_SESSION["errors"]=$errors;
header("Location: prevPage.php");
exit;
}Now obviously, I've left out a lot of details about how each method works, but I think you can see the outline and the steps involved in each. Do you have an obvious favorite, that you use every time? All of the methods can of course use functions to pull lots of the fluff out to a seperate file, for instance all the validation or the individual page displays, etc. All the methods could be OOP or not, so that's not an issue here. Are there other paradigms to be aware of?