Okay, so next up, I'd like us to see forms.
So we're gonna have a super simple form that will submit some data, and then we'll see.
How can we do something with this data.
Generally how do you handle the form submission.
So as previously let's add the logic section at the top.
Let's keep it empty for now.
And in the meantime let me add HTML tags and body tags that will display the form.
So both the form and the form handling code would be in this same form speech p file.
So now I'd like to start a server I've called this file forms PHP.
So we are now running the server and it's empty.
So that was expected.
Now let me add a body element and maybe I can start with a header Saying, um, please submit the form.
Okay.
And now we are adding a form HTML element and the action.
Well, let's add just a slash so it will submit to the current URL.
Anyway.
That's the default behavior of forms to submit to current URL.
So we can as well skip the action altogether.
Now the method.
This is important because by default the method would be get.
We want to change it to post.
And now let me add the field.
So I'm going to use the A label element to say that we expect an email.
And right after I'm going to add an input of type email that's a new valid input type.
And the name of this is email.
And this should be self closed.
And then at the bottom we can have either input of type submit or just a button saying submit.
So by default button would be a submit button as well.
And it will just submit this form.
Okay so I've made this form method post on purpose because that's not the default HTTP method on websites.
By default, when you just hit some address in the browser and you load a website.
This is making a request of type get.
So I'm using the same page.
And when this page is requested with the Get request.
So when it is being seen in the browser we display the form.
But if this page would be requested using the form post method I'd like us to instead expect the data
to be sent and handle the form.
So let's see how can we do that?
We need some kind of an if statement, that's for sure.
And I've mentioned this super globals previously.
Not without a reason.
They are really, really useful.
One of the things we can use this server for is to check the request method.
It's not suggesting that.
Anyway, there is a request method in this array, and I'm just going to check if this is post, because
if it is, we're going to do some handling.
I might as well die and say data was submitted.
Let me type anything in here okay.
So we've got the client side validation.
I've specified this input to be an email okay.
Data was submitted.
So indeed, this check works.
It means that this was now a Post request.
But when I refresh, uh, or when I just go to the page again and this is a Get request, then we see
the form.
Okay.
So now we know that this form can be submitted and that we can handle a Post request.
So we can do something with this data.
When you submit the form you typically would like to do something with this data, maybe store it or
maybe send an email.
So let's do something first.
Let's Vardump another super global.
This is called post.
Maybe you can guess what this post contains.
So yes, it will contain all the data that was submitted using the Post request.
Essentially all the form data.
So now let's try again.
I'm gonna send my email.
And what we get is an array where keys are all the names of the fields that you've specified inside
the form.
Actually, that's the name, name, email and the values.
Now, every single time when users submit some data, you need to make sure that you sanitize this data.
You make sure data is secure.
So you either need to validate the data before inserting it to a database.
You need to escape the data to make sure people don't try to send SQL queries, which you might accidentally
run in your database.
Now, with this email, we're gonna do a simple validation.
So we're gonna call filter var getting the email from the post.
And this should have a parameter That's filter sanitize email and this is exactly what we are doing.
Let me close this square bracket in here.
Okay.
So we just make sure that email is correct.
And you should never really depend on the client side validation in the browsers, because you never
know if the user's browser supports that.
And it's always your responsibility on the server to make sure that data is safe.
Okay, so finally we're just gonna say the email.
Email was submitted.
And maybe that's it.
Um.
We can also just die.
Okay.
So now let's go to this page again.
Let me type the email and there it is.