This video, we'll see how to submit our form to a PHP script. We'll also go over that script line by line just to bring us up to speed with some PHP basics. Okay, now that we have our form, what we're gonna need to do is add some PHP to handle the form submission. So on this opening form tag we're gonna need to add a couple of things we need to add in an action. This is where the form will submit to and we'll just call this newsletter example php just so we can have a little play around and test a few things. And we also need to add in a method, and this is going to be post and on each of our form inputs, we need to also add in a name attribute.
So this one will be name equals name and then this will be name equals email. So let's save that. And we are also going to need our newsletter example file. So if we come over here and add in a new file, and I've got a little snippet over here that I'm just gonna magically copy and paste in, you can um, go ahead and pause it and type this out if you like. But for now, just um, just come along for the ride. So let's go back to our website, give that page a refresh and let's see what happens if we go name.
Now come add example, submit fail. Okay, the problem that we are seeing here is our form is submitting to the wrong URL. If we go back, what we need is our form to submit to projects meat, yogurt. So if we go back to VS code, paste it in there. So now we're submitting to projects, meat, yogurt, newsletter example dot php. So we'll save that.
Go back to our website refresh. Let's try again. Now that we've got that working, let's just go back to our code and go over it line by line. So if we go back to VS code, we want our newsletter example and I'll just make that a little bit smaller so we can see the browser as well. And also I have some comments that I'm just gonna paste in down here just to help me with some notes. So the first thing that we should talk about is echo.
Echo is um, simply just outputting something to the page. In this case it's an H one tag that we can see over here. The next line is just echoing out a pret tag. And what a Pret tag does is just add a little bit of format to our code. So if I just jump down to here and comment out those two and refresh the page, we can see that now that code or that array has been squashed into one line. And if I undo those changes, it gets separated down and just makes it easier to read.
Okay, so let's skip this block and go down to the next one here. Be a little bit easier to explain. So on this line we have two things going on. We've got printer and we've got this post variable. So let's start with the variable part. A variable just it's just a container for something, it's a way to hold some information.
For example, we can create our own variables. So what we can do is we could grab this string, add it to that hello variable and then echo out. Hello. And fingers crossed we still get the same thing. 'cause all we're doing is adding this string into a variable. Then we are using it down here.
If we just update that just so we can see that it's working helps if you put your exclamation marks in the right spot, then we can see that Now we're updating that variable and outputting it here. So the post variable is a variable that comes with PHP and it contains the contents of your form. So when we submit our form from our website, if you remember we had a name and an email input. And these are the values that was sent across when we submitted the form. This particular variable, this post variable, contains an array. And an array is just a way of packaging up some data into a, think of it like a container.
And it just makes a nice easy way to pass it around your website. So if we have a look over here, we can see that this is an array and this is the data that it contains. And what Print R is doing is that, is letting us look into that array and output its contents onto the page, which is mostly just used for debugging. So you have some array and you're not sure what it contains. This just allows us to output it to the page, gives you a quick visual reference of what it contains and then you can go ahead and use that in some way. And like I said before, it's the pret tags here and here that give that some formatting that makes it easier to read.
On the next line here we are reaching into the array and pulling out a value. So it uses the key to output the value. So if we have a look at this array, we can see that we have a key of name and a value of Malcolm and we also have a key of email with a value of my email address. So here we're saying the post array, give me the value that's assigned to the key of name and if we change that to email and email and we'll save that and refresh the page, we can see that Now instead of outputting my name, it's output my email address. So also on this line we are using is set, I'll just copy that comment up there. So what is set is doing is is checking.
If this key exists on the array, then okay, go ahead and output it. And if it doesn't, this is kind of the default and it's saying just output nothing which is null. So if we try to, let's try to output something that doesn't exist. So let's say we out try to output a phone number. If we go back over to our website and refresh, so it's saying undefined index phone. So it's trying to look for a key that's phone.
So it's going, it's got a name, it's got an email, but no phone key. So it's throwing an error to let us know that. So if we just change if email to phone up here and refresh the page, we can see that it's outputting nothing, which is null. And um, it's not throwing an error, which is what we want. So if we were to just change that to no phone, we can see that it's outputting the default there. So the last thing that we need to talk about is the part that we skipped over before, and that's this get variable now get is similar to the post variable.
It just, it's just a slightly different way of passing that information through to the PHP script. Now to demonstrate that we are gonna need to go back to our website and if we go to VS code and back to the index now before we added a method of post. So this method refers to post that we saw before in the as an array. And what we're gonna change this to is get, if we refresh our website and enter in some information and submit, you can see that this time that our post array is empty. But a get array has some data now and the difference is, is get is passing that data through as URL parameters where post kind of did it as a hidden way in the background. Get actually displays the values in the URL.
Most of the time you're going to use post as your form action. It's just a nicer, cleaner way of doing it without creating big long. You can imagine if you had lots of form inputs with heaps of data, you're gonna end up with quite a long messy URL. But it does have its uses. Imagine if this was a blog and you had a category in your blog and you wanted just to link directly to that page and then filter all of the articles by by the author of Malcolm. You can see how that might be useful just to be able to link straight to that page.
But most of the time, like I said, if you're submitting a form, start with using Post. Okay, now that we've gone over the basics of PHP, I think we're about ready to start adding some data into the database.