Actually, you could have everything into one page and check which variables are true at any given time:
if (!$formData) {
// display form code here
// make a field in the form by the name of the variable in the control structure (if statement) above
}
else {
// validate input and allow user to go back if it is invalid
}
This could also work just as well using a switch statement.
For validating the input, it's as simple as checking each field individually using an if control structure:
if ($name == "") {
$badForm .= "- Name <br>";
}
if ($email == "") {
$badForm .= "- E-mail Address <br>";
}
This could validate a simple form with a name and an e-mail address (of course, feel free to go more extensively into validating the e-mail address with a regular expression.
Once you are done with the form, check if the variable $badForm exists. If it does, there's something wrong with the form. If not, the form must contain valid data. So simply do something like:
if ($badForm) {
echo "Your form contained errors <br>" . $badForm;
}
else {
// rest of code to process form
}
Happy PHPing 🙂