Check out a free preview of the full PHP Basics course
The "Post Parameters" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:
Maximiliano demonstrates the difference between the GET and POST superglobal variables. POST data is sent from the form through the headers in the request rather than on the URL. The "issset" method is used to ensure the data is sent before it's used on the page.
Transcript from the "Post Parameters" Lesson
[00:00:00]
>> Maximiliano Firtman: So this is a GET.
>> Maximiliano Firtman: The GET super global variables. Is there any other way to pass data between forms or between pages? The other way is POST. It's called GET because this is using a GET HTTP request. But if you have ever played with RESTful APIs and things like that, probably, you know we do have a post verb as well.
[00:00:28]
What happens with the data when you're using POST? So in the headers, you're going to specify that you will send parameters in a different format, and actually you send that in the body. So let's see how to do that. In the form, you add just another property method, and you say, you know what, I want post, I wanna use post.
[00:00:55]
So now when you use post, you won't see the amount in the URL. So it's a different way to send data to the php script. So now, if I go back and refresh and say 3 ETHs and convert, I'm getting errors. Okay, fine, by the way, this is how you see an error when we are in a browser.
[00:01:20]
In php, it's a warning actually. I don't see in the URL bar any data, so it's kind of hidden. You can see it if you look at the network layer. So, why it's complaining? Because yeah, undefined array key amount. What's going on here? Well, I'm getting from GET and I wasn't checking, okay, if I do have something there or not.
[00:01:48]
But I will do that check but with the different super global variables. So instead of GET I can use POST. And that's how you receive the data that was sent using POST from a form.
>> Maximiliano Firtman: Just that, you just changed the global variable. So if I go back and refresh, I'm back here.
[00:02:13]
But what if I open a new tab or a new window in Chrome and I go directly to convert php. So it's not coming from a form. I'm back to the errors because I didn't get, I never received any data. So I should check before using the data if we do have that.
[00:02:33]
Do we have if? Yeah, of course we have ifs. So how to check if the data exists? The simplest quick way to do that is isset. It's a function that will check if that one is set, and also the other one is set, so both, then I can move on.
[00:02:55]
Makes sense? So if they are not set,
>> Maximiliano Firtman: I can echo an error. And instead of opening doing an echo I can close php, open php here again and just right here I can do an image or a wherever. Oops.
>> Maximiliano Firtman: It didn't work. Try again, whatever.
>> Maximiliano Firtman: Remember, the same happens here.
[00:03:33]
I can close the php, instead of doing an echo, I can just keep the paragraph alone.
>> Maximiliano Firtman: But you can see the clarity of that code, it starts to fall. I mean, let's see if it works. So I refresh, hope it works. So it's kind of trying to do that, but I'm still getting the warning.
[00:04:03]
I mean, it's working, but I'm still getting the warning. That is because maybe I should ask for that before so I don't even try, but now I don't have the variables. By the way, this is also closing the. So I can check for this before here directly and put that if before.
[00:04:28]
Try getting confused, no worries, we will get there. So I will move the if at the top. So I'm not even creating local variables if I don't receive the data.
>> Maximiliano Firtman: Make sense? Now, as I mentioned before, this idea of opening and closing, php might be confusing. Yeah, it is confusing.
[00:04:59]
So for example, this else, typically we put it like this, and this one that is closing the if, we can put it like that. If you don't like this format, remember we have the other format, where instead of opening block, I can use column. And then instead of doing this, I use and if.
[00:05:19]
But for that, I need to also use the column here. Makes sense? It's still confusing. If it's confusing, it's fine, but it's normal this in php. That's why a lot of developers complains about php. I think that probably for this example you will still be more comfortable if we stay with the standard one, so with echo.
[00:05:47]
>> Maximiliano Firtman: Different ways to do the same thing, okay, so I'm not saying which one is better than the other. So let's stay with this so you will be happier.
>> Maximiliano Firtman: And I'm here for your happiness, right? So, okay, semicolon, I need to close the php tag. So it's complaining because body is not php.
[00:06:15]
By the way, you can open and close the tag at any moment in the line, okay? I think that looks nicer, cleaner. Okay, for you. But the other one, it was fine as well. By the way, this echo has parentheses, the other one has no parenthesis. Both are okay, okay, but let's try to stay using the same syntax.
[00:06:37]
So we've seen two superlobal variables GET and POST. What else do we have? Just forward in case you want to get deeper later. We have a request, that is an array with all the information and it mix get, posts and cookies in one bag. And by the way we also know and we are seeing there we have cookie.
[00:06:58]
We can read cookies. I'm not sure how many of you are comfortable with the idea of cookies, but a cookie is a piece of text that you can send to the browser and the browser will return that value back the next time is making a request to your server.
[00:07:17]
That's a cookie.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops