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 invalid information is submitted through our form, we now have an error message in a PHP variable. In this video, we’ll continue to enhance the flow of the form to display the message from that variable with the form.
-
0:00
We are now loading messages related to any errors we encounter into a variable.
-
0:05
Let's take the next step and
-
0:06
display the suggest form again, with the error message above it.
-
0:11
We began with a check to see if the REQUEST_METHOD is POST.
-
0:15
The conditional block of code ends with a curly brace here.
-
0:22
As we go through this conditional code, there are two possible outcomes.
-
0:26
First, the data could be valid, and the email could get sent successfully.
-
0:31
In which case, we would get redirected to the thank you message.
-
0:35
The REQUEST_METHOD would no longer be POST, and
-
0:37
so that block would be skipped entirely.
-
0:41
Second, an error_message variable could get set
-
0:44
on the suggest page in one of our four checks.
-
0:48
If there is an error message, our code will continue down here
-
0:52
past the conditional and execute the code to display the page.
-
0:57
Let's put some troubleshooting code in place at the top of our page just so
-
1:01
we can see the error message.
-
1:03
After our header, we'll say if (isset($error_message)).
-
1:12
Then we're going to echo $error_message.
-
1:19
Let's take a look at how this looks in the browser.
-
1:24
When I first load the form, the REQUEST_METHOD is not POST.
-
1:28
So we skip over the whole top block of code.
-
1:31
The error_message variable is not set, so we don't try to display it to this screen.
-
1:37
I'll complete the form but I'll specify an invalid email address.
-
1:49
When I click Submit, it reloads the suggest.php file.
-
1:53
Instead of redirecting to the thank you message, it now sets an error message and
-
1:58
then continues on displaying the page as before.
-
2:02
You can see the error message displayed here at the top.
-
2:06
Let's now integrate the error message into the site better.
-
2:09
We'll grab this conditional and move it into our form.
-
2:15
Here's our form.
-
2:18
And right after this else, I'm going to paste their error message.
-
2:25
I've included the CSS for a class named message.
-
2:31
Class="message".
-
2:41
Let's go back to the page and resubmit the form.
-
2:47
Now we can see the error message displayed here.
-
2:51
Looking at it now, it might be better to replace this
-
2:54
introductory paragraph with the error message.
-
2:57
We probably don't need both.
-
3:00
Let's add an else block.
-
3:04
And then we'll move our paragraph up inside our else
-
3:14
This way, our welcome paragraph will only show if there are no errors.
-
3:22
There.
-
3:23
Now, we have our nice error message telling us that the email
-
3:26
address is invalid.
-
3:28
There's one more change that we should make to our error messages.
-
3:32
If I submit the form with nothing filled in,
-
3:36
it still gives me the Invalid Email Address error.
-
3:40
This error is true but it's not the most meaningful error.
-
3:44
The most meaningful error would be that all four fields are required, and
-
3:48
that message should probably have the priority.
-
3:52
But how exactly is that priority determined?
You need to sign up for Treehouse in order to download course files.
Sign up