My favorite way of testing this is:
<?php
function getdata( $error_message='' )
{
global $PHP_SELF, $required_X, $required_Y;
echo $error_message; // no harm if message is empty
echo "<form action=\"$PHP_SELF\"..... >";
echo "<input type=text name=\"required_X\" "
. " value=\"$required_X\">";
...
echo "<input type=submit ...>...</form>";
}
if ($REQUEST_METHOD == 'POST') // form is being submitted
{
if ($required_X == '')
$err .= "You forgot to enter X<br>";
if ($required_Y == '')
$err .= "You forgot to enter Y<br>";
// more tests as needed
if ( $err == '' ) // no errors at all
{
// do something with the data,
// save to database,
// redirect to next page, etc.
}
else
get_data( $err ); // get form again
}
else // first time, not POSTED
get_data( '' );
?>
Bundling the form-drawing into a function means you can easily
call it from several places (orginal GET of the form, any time
your validation feels the form is not correctly filled out, etc)
without repeating the code. You do have to list all the form
variables a globals, alas, but at least that is also confined to
one place only.