FREELessons: 9Length: 53 minutes

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.1 Processing Form Input

The first step in handling form input is getting the basic data from the user. In this lesson, we'll take the input from the user and display it on the page so that it is visible.

2.1 Processing Form Input

So the first thing that we're gonna want to be able to do is go ahead and get the response that's being posted to us, or the data that's being posted to us in this form. So, how do we do that? Well, the basic way that we're gonna do that is through super globals again. So you already saw a little bit of a super global example over here with the underscore server, we're gonna use another one right now. So the way that this is going to work is, I'm just gonna come up here to the top of my body, and I'm going to inject some PHP code. So what I wanna do is I wanna say, all right, I have some code that I want to be able to use so that I can get that data and do something with it. And right now I really just wanna store the data that's coming in in that particular form and then I wanna show it down here. But I only want to do this if it is part of a post. So, the way that we check out to make sure that we are actually getting this data from a post, or that the request coming to this page is via a post is using that underscore server super global again. So we're gonna say if $_SERVER and you can say request_method, and we can check to see if that is actually equal to post. And if it is, then we wanna go ahead and grab the data that is coming through that post, and, how do we do that? Well, we use another super-global known as _POST. So what we're gonna wanna do is pull out these three pieces of information that are coming through the name, the email, and the comments. So let's go ahead and create three variables, we're gonna say $name is gonna be equal to, and I want to go ahead and get _POST and I want to get the name property. I wanna get the email property, so we'll say email is equal to $_post, this is going to be email. And then finally, I wanna get $comments is gonna be equal to _POST, and this is going to be the name of comments, so comments right here. So now, I have checked to make sure that this is a post, which means that data's coming across. Now I can use this super global _POST and then reference the name that is associated with these inputs here, right? The name attribute on this particular input element, and now I have them saved here. So now what I can do is I can come down to the bottom and I can just do a little bit of formatting down here. And I can go ahead and display this data to the end user. So let's go ahead and do some more PHP down here, and all I really wanna do is show this information. So we'll say echo $name, and then we'll just go ahead and put a break in here. We'll go ahead and copy this, and we'll do two more of those, and we'll simply say email, and we'll say comments, just like that. All right, so let's go ahead and save this and we'll go ahead and go back to our main page here. And as you can see, nothing has really changed, and just to kind of reiterate what I said before. If I view page source, you're gonna see that with this super global here of $_SERVER['PHP_SELF'] what it actually did is it got the name of the file that we have right now and we see index.php. Now what we also see is because I'm doing this refresh the page here, this is issuing a GET request to this file, the index.php. And because it's a GET request, it's skipping over this if block because we're checking to make sure that we're dealing with a post. So now, if I come in here let's just put in some information. We'll say Derek, we'll say and then hello, my name is Derek. And then if I do that and hit the submit button, now we're issuing a post request our index.php. We were able to extract using the _ POST super global. We're able to extract three pieces of information the name, email, and comments and then post this information down here. So as you can see, as far as retreating information and handling that data coming across on a form post to a particular page, it's really not that complicated. It really just consists of two parts, your checking to make sure that the type of request coming to that page is what you're expecting. So it could be either a GET or a POST, either way, but in this case we're using the POST method. So, we're making sure that we're dealing with a POST, and then we can go ahead and use the _POST super global to get that data. Now if you were dealing with a GET you would use the GET, but either way it will work. So now we can see how we're doing some very basic form handling to get data from a form and display it to the end user. Now, obviously you can gussy this up a little bit and show some additional information, but this is kind of enough to get us going. Now we see the basics of submitting the form and then handling it on the same page.

Back to the top