We could go through it one step at a time in a hypothetical scenario where a visitor goes to your form page and fills it in.
For reference, here it is again in one place.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { // The form is submitted
// do the validation here
// Then, depending on how that turns out...
if(isset($errors)) { // There are errors in the submission
// Show the user the form again, with errors listed
include 'html_form_template.php' ;
}
else { // Everything is cool with the form submission
// Send the email
// Record to database
// Show the user Thank You message
include 'html_thankyou_template.php' ;
}
} // end the form is submitted
else { // Nothing has been submitted
// Show the user the form for the first time
include 'html_form_template.php' ;
}
?>
First of all a visitor lands on your form for the first time. The php script starts to process.
The very first thing is this if
condition.
if ($_SERVER["REQUEST_METHOD"] == "POST") { // The form is submitted
}
This tests whether the request method is post (or not). As in: Has a form been submitted where the form method is “post”?
Because the visitor has only just reached the page for the first time and has not filled anything in, the test returns false
and the processing skips whatever is between those brackets.
But the closing bracket of the if
is immediately followed by an else
.
else { // Nothing has been submitted
// Show the user the form for the first time
include 'html_form_template.php' ;
}
So the script does whatever is in those brackets.
There we have a php include.
The include statement includes and evaluates the specified file.
The included file could be whatever you like, but in this case it would be an html document which contains your html form.
Therefore, that is what your visitor will see, on this occasion, your html form.
Next, the visitor fills in the form and hits the submit button.
Because the form element’s action
attribute contains reference to this self same script, the script loads again and is evaluated again from the top.
Again, we start with the if
condition:-
if ($_SERVER["REQUEST_METHOD"] == "POST") { // The form is submitted
}
Only this time around, the form has been submitted and the method was post, so the if
condition returns true
.
So the script does what is in this first pair of brackets, not the second else
pair.
The next thing we see is:-
// do the validation here
OK, that’s just a comment as a placeholder for the actual validation code which would be there.
I did not want to spend my time complicate things further by putting all that in there.
But just a little now to demonstrate.
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$email = $_POST['email'];
}
else {
$errors[] = "You did not supply a valid email address!" ;
}
This if
condition uses a validation filter to check if the input string looks like a valid email address. It saves you having to do some complex regex to find this out.
Supposing that the visitor makes a mistake and misses out the @ or something, that won’t validate so the condition will return false
, the first brackets get skipped and you move on the the else
where we find:-
$errors[] = "You did not supply a valid email address!" ;
This adds a string (error message) to an array named $errors
.
Note: This is not how error messaging/handling is done in some other examples. This is just how I do it. I’m not saying this is the only way or best way, just my way.
That other example you had defines a bunch of variables for various errors as empty strings at the beginning, then fill them in if the errors occur.
I am adding each error that occurs to an array. The array is not predefined, so will not exist unless one or more errors occur during validation.
That comes in useful for the next if
condition.
There will be more validation for the rest of the form fields which will return true
or false
and may or may not append the $errors
array with more messages. But one example of that is enough.
So on to the next if
condition.
if(isset($errors)) { // There are errors in the submission
}
This will return true
only if $errors
is set.
Because the visitor got the email address wrong, there is an entry in the $errors
array, so it is set, it returns true
.
And in those brackets we see:-
// Show the user the form again, with errors listed
include 'html_form_template.php' ;
That same php include again which displays to them the html form so they get another go at filling it in properly.
Because there exists an array containing any/all validation errors, they can be echoed out via a foreach
in the form page.
Note: That will involve ‘tainting’ your html with a bit of php, but ho-hum, it’s going to happen a bit at some point, just try keep it to a minimum.
The visitor now sees the form again, with a list of errors telling them what they did wrong, so they can correct and resubmit.
And the script starts over again.
if ($_SERVER["REQUEST_METHOD"] == "POST") { // The form is submitted
}
This is true
again. But this time all validation gets passed. Nothing gets added to the $errors
array, in fact, the $errors
array never gets defined, it does not exist this time.
As a result…
if(isset($errors)) { // There are errors in the submission
}
…returns false, so those brackets get skipped and you move on to the else
.
else { // Everything is cool with the form submission
// Send the email
// Record to database
// Show the user Thank You message
include 'html_thankyou_template.php' ;
}
First we have this:-
// Send the email
// Record to database
Again, just comments as placeholders for the actual code that will perform these actions, quite self explanatory. No need to go into that just now.
Followed by another php include:-
include 'html_thankyou_template.php' ;
But this time to a totally different html document. Not the form, but the “Thank you” page.
Because we are still in the processing script, not redirected to another page (though displaying to the visitor a separate external document file) all the post data and stuff parsed from the form is still available, so could be echoed out in the html page if required.
The form process is now complete.
If that does not explain it, I don’t know what to suggest. A good book or php course perhaps.