Now let's go back to this contact post php file.
That's a route handler that handles the submission of this form on the right.
Let's see how does it look like when we submit the form.
So I'm typing anything and I'm gonna just zoom in.
So you see this message.
And we have to say and agree that that's not the best user experience.
We are basically leaving the user in a limbo.
He has no clue what to do next.
There is no button.
The page looks different.
This is not how you do things.
This should either be a button to redirect to another page or automatic redirect.
Or even better, we should just redirect to the page which displays this messages so they can see their
message right away.
Plus, there should be some message that is clearly visible, and it can be distinct from the rest of
the page that either something was successful like this case right here, or that there was an error.
There is no point in having a separate page with a message.
So this is sometimes called flash messages.
It's a message that you display only once for the next request on the next page, and after it is being
read by the user, it just disappears forever.
We're going to be implementing flash messages in this video using sessions, and let's get to it right
away.
So first let's maximize the coding space because first we need to implement some logic.
I think we can just add another file inside the includes folder and call it flash.
I don't think it belongs to any of the other categories.
This would be a PHP file.
So first let's add a function that will add a flash message.
And let's keep the type we need to separate between the success messages and errors, because later
on we need proper colors to display those messages.
And it needs to be clear to the user what has happened.
Then another argument here is the actual message that void nothing to return from it.
And remember this super global session variable.
So we're going to add a flash entry into it.
Then another type and we're gonna add the message.
Now, you might want to add more than one message of a specific type.
I just think that for this app, it doesn't make any sense.
So after we add a flash message, next up we need a way to read those flash messages.
This would be called get flash messages.
It returns an array.
So we are fetching those messages from the super global dot session flash.
If there are none, we return an empty array.
Next, an important part.
If you read the flash messages and you do when you call this function, and we assume that they were
displayed someplace, we remove removed them.
For that, we use unset to remove an element from the session array that is under the flash key.
It's important that we will remove all the messages as in here in this file, we have no way to verify
if they were read or not.
And finally we just return the messages variable.
Now at this point let's jump to bootstrap.
We need to include this file.
So let's add an include for flash PHP.
And another thing that we have to do is obviously those messages need to be displayed someplace.
I think the best place is in the header.
Let me scroll all the way to the bottom, and I think we should display them right after the navigation
menu.
So right below the menu before the H1 element.
So this is right here right under the main element.
But I think that we shouldn't do that inside the header.
The better way is to create a partial template.
We can create another file called just Flashfxp and UN convention in many frameworks where you have
partial templates.
So templates that are not rendered for a root but are reused in other views is to start the file name
with underscore.
That way we know this is not something that should be used on its own.
It's always part of something else.
Now in the header I can just require this file.
So I do PHP require once And since those files are in the same directory, I can do it this way.
That's it.
Now another thing in this file the header is some styles.
So we're not going to be writing CSS styles I'm gonna just paste them and you can grab them from the
link under this video with all the code changes.
So we've got four CSS classes that need to be added.
I'm just gonna reformat that and that's it.
Okay.
Now we can close this file and let's go back to displaying the messages.
The first thing to do in this flash partial template is to grab the messages.
We will create a messages variable and call the Get flash messages function, which as you can remember,
will also remove the messages from session if there are any.
Next up, we only want to display those messages if messages variable is not empty.
That's why we add an if statement that checks if empty returns false, which means it's not empty for
messages variable.
We will display this specific block.
And inside I'm adding a div element that's a container for the messages.
And the class here we're going to use flash messages.
So this is coming from from the list of those CSS classes that I've asked you to paste.
And you don't really have to do that.
This is just styling those flash messages the way that we can distinguish them visually.
So you don't really have to worry about that.
And now we just need to go over every single message.
That's why we do for each messages us.
And we've got a key here.
Remember that there is always a type.
So we do type and message colon.
We need to close this.
So we do end for each.
And then inside let's add a div element with another class.
This is flash message.
And additional class that is of the specific type.
So here we need to output something from PHP.
We're gonna use HTML Special chars to output the type now inside it.
We are just left with outputting the actual message.
Also escaping it with HTML special chars just to be safe.
So this is a message.
And we also want to escape quotes.
And we are using Unicode.
And also I am missing a hyphen here in the class name.
So this is flash hyphen.
And then whatever is being outputted as a type of the flash message.
Now also notice that this is rendered very strangely in my editor.
This is actually a hyphen.
And this character more than or start of the PHP tag.
So just place together.
This is how my editor renders it.
That's not what you should be looking for.
Anyway, save the changes and we should move out of this file because we need to add some messages to
be displayed.
That means we need to jump back here and to this contact post file and instead call add flash message.
So the type would be success.
And I'm just gonna move this message into a flash message.
We can remove this and this as well.
Now in case of an error, I think we can also add a flash message saying exactly the same as whatever
was here.
So the same server error just displayed as a flash message.
Just make sure the type is error.
Now in both cases we should redirect to another place.
Now in PHP to redirect the user, you're gonna be sending a header called location with some URL.
In our case, this is probably guestbook.
That would be the best idea.
So this is how it's done.
You just return a header HTTP header that's called location with the value of the URL.
And you stop the execution at this point.
And I also think that this should be a function.
So let's move it to router because because it is about routing.
And let's just call it redirect because this would be a handy little function.
And let's just accept the URI and it is void.
Doesn't return anything.
And let's make sure we use this URI right here.
Okay.
Going back and let's do redirect to forward slash guest book.
You know what?
This is actually not correct.
We need to redirect here as well if there is a success.
Because otherwise this flash message the error one would be always added.
Okay.
So we need to call redirect in two places.
No big deal.
And one more thing that I've noticed I haven't closed this attribute.
So we need double quotes here as well.
Now it should be fine.
Let's close that.
Let's jump to the form.
Let me add any message and send it.
Here's how the flash message looks like.
It's inserted on the guest book.
I can see here that there is my message.
This confirms that it was successful, but if I go to any other page, it disappears.
This is a one time flash message only available for the next request.
So how do we test the case when something goes wrong?
It's easy.
Temporary.
Let me just add a false here.
So this never runs.
So we are forced to run this code.
Now let me type anything send a message.
There it is.
It's also a one time message that will disappear next time we do another request okay.
So let's bring back the functionality and we are done.