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?