 Hey everybody, this is Brian and welcome to the 20th LAMP tutorial. This is the beginning of what I will consider the advanced series of LAMP and PHP. Today we're going to be discussing forms, the get variable, the post variable, and the request variable, which isn't shown on W3Schools, but they do mention it. So to cover all these, what we need to understand first is what we're actually talking about. And as you can see, there is a get variable and a post variable. What's the difference between these two? Well, to really understand the difference, you've got to know how websites work. Here's my website, boydrums.com, and we're just at the default page. If you click on tutorials, and then let's just say click on LAMP, you notice how up here you get this question mark filter equal LAMP. That's called a query string. That is what you would use in PHP with the get variable. Now, the example they give you here is the post because we're on forms, but let's go to get real quick here. So they say PHP echo getfname. Well what's fname? In my case, it would be filter. And then it would give you the value, which in this case would be LAMP. So let's actually search for something. That's how I can search, and yet the query string doesn't change. That's called a post. What we're doing is we're posting data to the server. What's the difference between the two? This would be a query string used in an HTTP get, meaning we're getting something from the server. Post, when typically used when we use a form, we're posting to the server. And it happens in the background. You really can't see it. We could actually go to view page source. And right up here, most programs will strip it off. But in the HTTP request, we're actually posting data to the server. The server strips it off and then sends this the response. Let's go back here. So what's really the difference between the two? Well, one uses a form, not all the times, but typically you use a form when you post data. You post data when you don't want the end user to see what you're doing in the background. You use the get or the query string. When you want the user to be able to see it or interact with it, for example, if I wanted to bookmark this page, what happens if I take this off? You guessed it, just dumps you right back. Because this is the same page. Look, tutorials.aspx. Tutorials.aspx, but now it's got a query string. Meaning there's magic happening in this page, for lack of a better term, that tells us, hey, filter the results based off the query lamp. And I know it's bad form to show a .NET page in a PHP tutorial, but it's the best I had on short notice. So now you understand that the get variable will get the query string and the post variable will get things posted into the page. There is also a third one. Let me find it in here, called, there it is, request. PHP request variable. Request is a combination of get, post, and cookie. We haven't covered cookies yet, but that's coming in another tutorial. A cookie is a simply you store information on the end user's computer so that when they come back, I'm sure you've seen that like on Facebook or any other website where you say, you know, you log in and you say remember me. So that when you come back next time it doesn't have to have you log in. That's a cookie. But the request variable is actually a combination of all three of those. So you don't have to remember which one's a get, which one's a post, which one's a cookie. You can just use request. All right, okay, loaded up our IDE here. I had to pause it because I forgot to open it. It takes a little bit to start because I'm on a virtual machine. What we're going to do now is we're going to show off the get variable. So we're just going to simply say get name. We're just going to get the name here. So we're going to echo this out. Now, where is name? I mean, if we save and run this, nothing happens. Where's name? Well, if we add a query string name, whoops. Notice how suddenly it appears. That's because the get variable. Notice how this has the exact same syntax as an array. You guessed it, what's going on in the background is it's PHP is actually creating an array in a variable named get. For example, let's just echo out the get variable and see what it says. Array. So that's all it's really doing in the background is it's taking all of the query strings and then you guessed it, generating an array. Let's actually do a little better example here. So we're going to get a couple of these. So we're going to say fname and lname. And fname is going to be the first name. Maybe if I could type, there we go. Save this, there we go. Now you see we're printing out two different things. We have our fname parameter and our lname parameter separated by an ampersand. Notice the query string always starts with a question mark. If you're ever looking at a web page and you see this dot PHP or HTML or whatever and you see this question mark right here, you know, right then and there there's some magic happening in the background. Usually it's PHP, Java, ASP, something along that name, along that nature. So always look for that question mark if you're submitting something. Then you know that you have a parameter equals and then a value. And they're always separated by an ampersand. We can even go one step further and say age equal and then whatever your age is and we're going to just grab this. Oops, I forgot to do that. Hold on, I'm a little challenged today. I've been multitasking too much. There we go. So now you see we've got multiple items in our query string and we can access those through the get variable. Now let's actually do a, let's do an HTML form. Let's just for the sake of argument here, let's go up here and let's just copy W3School's example, save me some typing. If you don't know what a form is, I would almost command you to go out and learn what it is because it's a very big part of working with dynamic web pages. I do have a HTTP tutorial and I do cover forms. Notice how we have form action and then the, if I spelled it right, the name of the page, tut20.php, which is the name of this page. Method equal post. That's how you know you're posting information to the website. Now I'm going to change this around a little bit and I'm going to say. We'll say full name, guess age. Save this out. Now suddenly we have a form here. See what it says, name, age, and the submit query. Now in the background I've named this full name. So this is your full name and this is guess age. Reason why is we'll have F name and age here. So I'm just going to say Brian age, we'll see 38, submit and nothing happens. Why? You notice how also our query string went away. Let's back up there. It went away because, well, we're posting, not getting. See the get variable, but we're posting. Now what we're going to do is make another PHP block here. Come on, kitty, you're going to have to move. Sorry, my cat is on my keyboard. It's driving me crazy here. There you go, buddy. He did not want to move, that's for sure. All right, so now we've got that. We're just going to grab these, change these to the post variable. And you guessed it, post is just an array, just like get. We're going to say full name and guess age. Get rid of that guy right there. So now when we submit our query, you see how it says Brian and 38. Let's say Chad and, let's say Chad's 21. Chad 21. So that's the difference between a get and a post and how you interact with those two. Now we're going to show an example of how to use the request array or the request variable. And remember, these are automatically filled in for you. You don't have to do a thing. In case you're wondering which one is more used, pretty much I've seen most people use requests, but sometimes people get picky and use post to get depending on the situation. So let's actually grab all of these to the magic of copy and paste here. We're going to just do that. Now if you've ever seen this before, like if you've been on a bank site and says, you know, the page has expired, you've got to re-send. That's because you're posting. So now we're re-sending the information. Let's see everything. Well, the only thing we have, because we don't have a query string, is we have Chad 21. So we're seeing Chad 21. And it's just printing out nothing for these. Let's see if we can actually do both at the same time. I'm not quite sure if I can do that in Komodo IDE. Let's say fname equal mark. Yeah, it's not going to let me do both. But you get the picture here, how you can use the git to get query strings, post to get post to the page. And then request is just an array of everything. Well, that's all for this tutorial. Like I said, it seems like a complex topic, but really all you have to understand is the difference between get and post. And then understand that the get variable, the post variable, and the request variable are just arrays that hold that information. Thank you for watching. I hope you found this educational and entertaining.