7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Handling the POST

After we have created the basic form, we need to be able to get the data that is being posted to our site from the form and do something with it. Before we actually do anything with it, like writing it to a file, let's create the basic structure and a function that we can use later.

2.3 Handling the POST

Now, we're gonna take our last step before we actually start working with files. So what I wanna do now is I want to handle the post. So that when you do fill out this form and actually post it to the server, to this index.php file, that you can actually see the data coming through, just to prove that it's working. So let's go back into our code here, and what we wanna do is we wanna do a little bit of processing up at the top of our body. So up here, what we wanna be able to do is say, php, cuz we wanna write a little bit of code. And what we wanna do is we wanna see if when the user came to this page, did they get here via a post. Meaning, did the user fill out the form and then hit the button And send data to the page. And the way that we do that is via a super global, and we're going to use the _SERVER super global. And we wanna look in here and check the request_method. So in here, we're gonna be able to tell whether or not this was a post, or a get, or a put, or whatever, and really I only wanna be able to worry about right now is if this is a post. So now that we have this super global, let's go ahead wrap this in an if. We can say, if the request method is equal to POST, which means that the user got here via a POST. And if that's the case, then we should have some data that's associated with the names that we specified in our forum via name and dream. So the way that we'd do that is we're gonna go ahead and get the name, and we're gonna set that equal to the value that's found in another super global called POST. Now, within post, that's where we can actually pull that data out by name. And we have two of them, once again, we have name, and then we also have dream. And we're gonna go into post again and we're gonna get dream. And then for now, if that's the case, let's just go ahead and output this to the screen. So we'll just say something like echo, and we'll put in here maybe an h3 and we'll say or we will go ahead and concatenate this and we'll say that this is going to be name. And you can do this concatenation anyway that you want. I'm just gonna do this manually here very quickly and we'll say another h3 here. And this one is going to be for the dream, and then we'll go ahead and close that off, just like that. So let's go ahead and save this. So now, if we were to post this data, hopefully, we have it in there. We're not doing any checking to make sure that the user filled those values in. That's for a different exercise. But all we really care about right now is if it was posted, let's go ahead and display that information out on the screen. So let's go ahead and go back into Chrome, let's go back to our main page just so that everything is refreshed. And now, if I were to put in here Derek, and let's say, To teach millions, and hit Send Your Dream. Now, you can see here I did get to Derek and I did get to teach millions. So now we're getting that data in there and now Now we wanna be able to do something with it. So let's go ahead and start to frame this up a little bit. So what I wanna do is I wanna be able to take that data, like we're doing right here. And then I wanna pass it off to a function so that I can actually use this from different places if I want. So we'll call this function catch_dream. And then within here, I'm gonna pass in two things. I'm gonna pass in a name, I'm gonna pass in a dream. And then within here, I wanna do something with it, and that's where we're gonna start to do the file handling. So then, at this point, all I need to do once I have grabbed that information from my post, then I'm gonna call catch_dream, and I'm going to specify name, and I'm gonna specify dream. So I'm passing the correct data into my new function, let's go ahead and save that. So now we have this nice structure going for us. Now, in the next lesson, I'm gonna introduce how to actually start to deal with files using one of the more common methods, and then we'll build onto it from there.

Back to the top