<?
$submitted = "";
test_state();
function test_state()
{
if (isset($_POST['submit'])):
global ($submitted);
$submitted = true;
validate_form();
else:
display_form();
endif;
}
function validate_form()
{
$validity = false;
//test validity and set to true if ok
if (!$validity):
display_form($msg); //put feedback in $msg
else:
save_form_date();
}
function save_form_data
{
//trim and clean all incoming data
// write to the database
if (!$result):
display_form("Database Error");
else:
display_comfy_message();
endif;
}
function display_comfy_message()
{
echo "Your data has been properly entered into the database";
/*
header("Location:newpage.php"); //and set newpage.php to contain whatever you want.
this second method should be coupled with unsetting $_POST and a unique identifier in the form to avoid duplicate db entried being created by the back button + refresh
*/
}
function display_form($msg="")
{
echo $msg;
global ($submitted);
if ($submitted):
extract ($_POST);
endif;
$erlevel = error_reporting();
error_reporting (0); //suppress errors to avoid notices for unfilled variables.
/*
now insert form elements using the notation
<input name="element_name" type="text" value=<?=$element_name?> />
of course this only works for text, hidden, password and textarea input types. you should also do similar things for radio groups, checkboxes and selects of course. these are easy to do once you get your head around the concept. check out the pear class HTML_Quickform which handles all this for you
*/
error_reporting ($erlevel);
}