Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
When information submitted through our form is invalid, we currently display an error message on the screen and stop all other code from executing. In this video, we will enhance the flow to set a PHP variable instead, which we can then display later with the form.
-
0:00
Back in workspaces, the code that validates the information entered
-
0:04
into the form can be found in suggest.php near the top.
-
0:10
Inside the conditional that checks if the request method is post.
-
0:19
If one of the fields is blank, we echo out this message and
-
0:22
exit, stopping the rest of the code from executing.
-
0:26
Instead of echoing out this message,
-
0:28
let's put it in a variable called $error_message.
-
0:37
We'll also remove the exit.
-
0:41
We have three total checks.
-
0:42
One to check that the required fields are entered,
-
0:45
one to check that the spam honeypot field is left blank, and
-
0:49
one to check that the email address is a valid format.
-
0:52
Let's make these same changes to the other two checks.
-
1:09
We used to execute when we found error in one of these three checks.
-
1:12
So we only get to the next block of code if we did not encounter an error.
-
1:17
But l will removed the exit commands, so that's not the case anymore.
-
1:21
We now need to write a conditional around the next block of code,
-
1:25
to make sure that it only execute when there are no errors.
-
1:29
We can check if the variable error message is set.
-
1:32
(isset($error_message).
-
1:42
If it's not set, then we know that we did not encounter an error.
-
1:48
So let's add our negation operator.
-
1:52
We will then execute the code to send the email.
-
1:55
Let's indent the rest of this code block.
-
2:06
We only want to execute the code to send the email if we did not encounter an error
-
2:11
in any of our three checks.
-
2:14
Inside this block of code, it's still possible to hit another error.
-
2:19
When we call the send method it will return false if there is a problem,
-
2:23
if there isn't problem, we want to send an error message,
-
2:31
Instead of echoing out the error with the exit, we'll send our error message and
-
2:35
once again, we will remove the exit.
-
2:39
Before, we only go to this code that redirects to the thank you message
-
2:43
if we did not have an error.
-
2:46
Now we'll get here anytime.
-
2:49
So lets move things around a little.
-
2:52
Lets change our conditional to check if the call to the send method
-
2:56
was successful.
-
2:58
It's good practice to end the script as soon as possible
-
3:01
to make it easier to follow the flow with less code.
-
3:04
Since our redirect essentially ends the code on this page,
-
3:08
let's move that code to handle the redirect up inside the conditional.
-
3:34
Now if the call to the send method is successful, we do the redirect and exit.
-
3:43
If it's not successful, we set an error message, and
-
3:46
then continue with our script.
-
3:49
We could put the error message in an else block, but that's not necessary
-
3:53
because we are leaving this page completely if our condition is true.
You need to sign up for Treehouse in order to download course files.
Sign up