Welcome guys to the next php video. And today, I'm going to be going over to get and post in php.
Now up here, you may have noticed on sites like Google that when you search, it will pop up here and type, put in your search query and you will usually end up looking something like that or something along the lines. And also, I'm going to show–that’s basically is the get value. And I'm also going to show you the post value which is where you might input something on a form and then press go. And then it processes that post data on the next page. So, this will open up some more possibilities, you’ll be able to do in php. So, let’s go and get started.
I'm going to ahead and navigate to my projects directory which is where I'm going to be saving the file that we used today. And go ahead and open up notepad or whatever text that you’re using and let’s get started. Now, I will go ahead and open up a basic php document. And what we want to do is first, we’re going to use the—we’ll use get just to show you what you can do. Let’s go ahead and do a simple HTML file. If you haven’t watched my HTML videos, you can go watch those to learn how to do HTML simply. Now in the body, what we’re going to be doing is creating a simple form. And for the action, we want the action to be the same name as the form one right now so we can send the post data or the get data back to the same page.
Now to do that, you open up php and you type “echo $_SERVER [“PHP_SELF]”, and that right there is what does that. It's basically echoing the page back to this area. So, the action is going to be whatever page we saved this at. So, if we save it as test.php, then it's going to put that back down there along with the http::// all that stuff. So, that’s the action. We’re just going to have it return to the same page. You can set this to whatever page you want to process the data on. So, if you want to process it on different page, then set it to that page. The method we’re going to start with get which would be the first method we discussed. And we’re going to create an input field, type = “textz”. We’re going to create the name of—we’ll use “q” since that’s a good example. And yes, that’s all we need for the input. We’re going to the submit button, input type = “submit” name = “SubmitForm” and we’ll put value as “SubmitForm”.
So, now that that’s complete, we want to go ahead in our form tag. And we’re going to save it. I'm going to save it in my project’s directory as get_post.php. Save as type, change to All Files and click save. Actually, when I open up our internet browser and find where that’s located, and click it. So now, we have our form here. And up here in the URL, we can see it just get_post.php.
Well, let’s type something else say “Hello World” and submit form. Now, it brings us back to this page because that’s what we told it to do. But now, you’ll notice up here in the URL we have the get values of our form. So, the first value is the value of “q”. It’ll put the name, the equal sign and then the value inputted for that. In this case, Hello World, you can see a plus between it, that’s because it's URL encoded which means it will take all the special characters like spaces and different weird symbols. And then that will convert them to URL encoding. And the space character in URL encoding just happens to be a plus sign. So, Hello+World and then it will put more names. Submit form, that was the name of my submit button. And they will (=) and you can see a (+) because I put a space, Submit+Form and then and then another (+).
So, it's basically just taking all of the form values with the name and returning them up here to the field. So, we have them in the URL now but how can we use them? Well, notice that we just had a name, “q=Hello+World. We’re just going to use php to get the value of what each name is. Now, just going over really quickly, you might notice this question mark here and then it's an “and sign” here. Well, basically, the first value that’s returned whatever it is, we’ll always use the question mark and then the next value as well as you put the and sign and “blah”, a “blah” and “blah” equals “blah”, and then use the and sign from there on out. So, just note that. It's nothing to get all hiked up over.
Now, going back into our php file, we’re going to open it up using the new one that we put in here. We have our php opened up here before the HTML starts. Well, let’s do echo and then to get values, you want to use the $_GET and that’s a reserved type of variable in php that’s going to be used to get the values. And you need to use brackets here because it's virtually in the array using all the get values found in the URL up here. So, we’re going to get the value of “q” and that’s all. It's going to echo the value of “q”. that’s basically what it's saying. Now, let’s go ahead in the URL of this page up here. We’ll type Hello World and submit the form, and it wrote it up there. That’s because it got it from the URL where the question mark q=HelloWorld is. And you’ll see that plus sign is going because it automatically URL decodes it when it uses the get data in php.
So, it's just taking care of extensive software so you don’t have to do it yourself. And—so there it is, Hello World written on the screen right there. Underscore get and then you put “name” and that’s how you get data from the form (echo $_GET [“q”]). Now, if we want it to, we could also—we’re going to link it with a “
”, and then link it again with—do you get the value of submit form (echo $_GET [“SubmitForm”]). I don’t know why I'd want to do that but if you do want to do that, well, that’s how you do it.
So, save that. I'm going to reload the page without that on there. And then we’ll type Hello World again, and there is Hello World and then SubmitForm which is basically just the value of this button. Now, the only bad thing about GET is, people can change it up here really easily. So, if you have some sort of special thing that you're doing and you don’t want them to be able to change it, then don’t use GET. So, like I can type “blah”, “blah” and then Enter and when it loads, it's going to tell you “blah”, “blah” because php is just getting that value that’s up there. And basically the form is set to the method GET so that it will automatically place that value up there for you when they presubmit.
So, that’s basically the GET. Now, we’re goingnna show you the self or the post fashion which is more secured but you still need to be careful when using it. And make sure that what they put in there can’t be altered in a way that’s going to screw something up. So, just keep that in mind and we’ll go over some more security later. But post data, if we set the method of the form to post, then basically instead of putting the information up there in the URL, it's going to send it anonymously over the server. It can still be retrieved but it's not in plain side. You would have to go through other extensus to get it. Get the information that’s being set.
So, it has the method of post here. And now, we’ll just basically—just like I explained a second ago, all of these is going to be sent over to the server. And you’ll notice that if we run it now and type Hello World. Oh! I forgot to save it. Okay now, if we run it, you’ll notice that nothing happens here. And you’ll notice that nothing is in the URL anymore. That’s because the data was sent over to server and we can no longer use the get function to get the information since it’s not in the URL. What we want to do is use the closed function. So basically, just change all instances of “get” to “post”. And you can get the data that was sent to post.
Now, let’s go ahead and save this and I’ll show you. Type Hello World, and there they are again, they popped up like normally but now they were sent over to the server instead of being completely visible in the URL. So, that’s basically the get and set function. You can do a lot more with this later and we will get into that. But, thanks for watching the video and I hope you guys enjoyed it. More php videos coming soon!
Transcription by:
Scribe4you Transcription Services