I believe the easiest approach would be to keep track of the progress with a $progress variable. Keept it as a session variable or set a cookie, or send it along in a hidden input field. Also, I wouldn't use the location header for this.
Can be put in another file and "required"
function handleForm1() {
// input validation and sanitizing as needed
// ...
if (!$validationOK) {
return getForm1();
}
if (isset($_POST['age']) && $_POST['age'] == 'yes')
return getForm2();
else
return getForm3();
}
function getForm2() {
$str = '<form ...>';
// using hidden input to deal with progress. This is the second form.
$str .= '<input type="hidden" name="progress" value="2" />';
// ...
$str .= '</form>';
return $str;
}
function getForm3() {
// ...
$str .= '<input type="hidden" name="progress" value="3" />';
// ...
return $str;
target file of all the forms' action attribute
if (empty($_POST['progress']) || $_POST['progress'] == 1) {
$nextForm = handleForm1();
}
else if ($_POST['progress'] == 2) {
$nextForm = handleForm2();
}
// ...
else {
$nextForm = getForm1();
}
echo $nextForm;
And if you need to carry over data from one form to the other, send the saniztized user input along as either one array parameter or several separate parameters in the call to the getFormN() functions.
Especially in the case when validation wasn't ok (some info not filled out for example), you would want to
1. Display all previous user input
2. Inform the user of what's missing