‫All right.
‫So now we're going to look at the post Superglobal which holds data that is submitted from a form that
‫is a Post request.
‫Okay.
‫Now you can also have forms that submit data and are it's still a Get request.
‫However that's not very common aside from maybe like a search, like a filter where what we just did
‫in the last video with the location that might come from a form that may be a Get request, because
‫then you have you want the data in the URL with a Post request, you get the data from the form and
‫it's not in the URL.
‫It's much more secure.
‫And that's what you're going to want to use if you're submitting to a database or something like that.
‫All right.
‫So I have this this form here where we can enter a title and a description.
‫This is in the starter sandbox.
‫So you can grab this if you want.
‫Basically what I want to do is be able to submit the data and get it in our PHP file using the post
‫superglobal, and then display the data on the page.
‫Now, the first thing we have to do when submitting data is on the form tag.
‫We need the method.
‫If you don't have the method, the default is going to be get okay.
‫And like I said, it can be post.
‫It can also be get.
‫We're dealing with post.
‫So we want to add that.
‫Then you can add an action.
‫And that action is going to be the file that this submits to.
‫So you might have a submit to an external file maybe called process dot PHP.
‫So if I come over here and I try to submit that it's going to say it can't find process PHP.
‫And that's because I don't have that.
‫It doesn't exist.
‫Now in many cases you want to submit to the same file.
‫And that's what I want to do.
‫I want to submit it to this index.php.
‫So what we can do is just leave off the action.
‫And if I do that and submit, you'll see nothing happens.
‫But I'm submitting to the same page okay.
‫So I'm going to just leave that action off.
‫So up here let's create some PHP tags.
‫And let's get the data that's submitted.
‫Now, when you're dealing with post requests, you're going to want to check the request method.
‫Okay.
‫And if you remember with the server Superglobal there's there's an array value called request method.
‫So that's what we're going to do is check that.
‫So let's say if and then we'll say underscore underscore server.
‫And we want to check the request underscore method.
‫Okay.
‫Actually, before we before we use it in an if statement.
‫Let's just.
‫Let's just echo it.
‫So if I just reload the page, actually, I don't want to reload.
‫I want to go to the page, then it's going to be get.
‫If I click on this button here and submit the form, then it's post.
‫Now if I make a post request and then I reload, it's going to confirm that the form submission.
‫Click continue.
‫And it's still going to be post.
‫So to go back to get I'll just go to the page.
‫All right.
‫But back to our if statement.
‫Here we want to say if the request method is equal to post.
‫And we also want to check to see if a specific value is is submitted.
‫So if a specific field is submitted what I usually do and there's a lot of ways to do this is on the
‫button.
‫Right.
‫So where is our button right here.
‫So it's a button type submit.
‫And notice it has a name attribute.
‫In fact all of these have name attributes.
‫And that's really important because that's how you get the data on the PHP side is with this name.
‫So what I usually do is check for that submit.
‫And we can do that by saying let's have a double ampersand here and let's say is set.
‫And then we'll put in here our post and then submit.
‫Okay.
‫And whatever I'm putting in here is the name of the field in the form.
‫So if I want to get the title I would put title in there.
‫I just want to check for that submit, make sure that it was actually submitted.
‫So if that is true then whoops what is going on here.
‫If that's true then let's do a var dump.
‫Of just post.
‫Okay, so I'll actually put some stuff in here.
‫We'll just say.
‫Uh, I don't know.
‫Listing one.
‫This is a listing.
‫And then I'll click Create Listing.
‫And you can see I've Vardump gives us an array with three three items.
‫Title listing one description.
‫This is a listing and submit which is just.
‫It comes in as an empty string, but we're making sure that that submit is clicked before we do anything
‫with that form data.
‫All right.
‫Now let's put the title in a variable.
‫So we'll set that to post and title.
‫And we'll do the same with what's the other one content.
‫Let's see.
‫Text area name is description.
‫So let's say.
‫Description and we'll change that variable to description.
‫Okay.
‫And then just to test that we'll echo title.
‫And with echo we can put more than one value.
‫So we'll do title and description.
‫If I reload it's going to submit it again.
‫And you can see we get both of those.
‫Now a couple things.
‫First we're going to want to check to see if that post title and description are there before we put
‫them in variables also.
‫I showed you a little bit of this in the last video.
‫We want to protect against cross-site scripting attacks.
‫So right now if I were to put in here let's say script and I'll do an alert and let's just pretend this
‫was malicious code, I'll say you have been hacked.
‫And then close that script and let's go ahead and submit it.
‫And there we go.
‫That JavaScript runs.
‫So obviously we don't want that to happen.
‫And it's running because I'm putting it on the page.
‫So and ultimately I am going to want to put it on the page down down below here.
‫So what we can do is where we set the title instead of just setting it directly to post title, let's
‫first see if it's set.
‫So we'll say if is set and let's say post.
‫Title.
‫Okay, so if it's set then we're going to use a ternary operator here.
‫Then I want to put into that title variable the value.
‫But I want to run it through HTML special chars.
‫So I'll do HTML special chars and then pass in that post title.
‫All right.
‫And then if it's not there.
‫So let's go after this parentheses here and let's say else then it's just an empty string.
‫All right.
‫And we'll do the same for the description.
‫Like that.
‫Okay.
‫Now, if I were to pass in here a script.
‫We'll just say alert one.
‫And if I submit that it just prints it on the page.
‫Now we could even we can shorten this by using the null coalescing operator.
‫So I could actually do title equals and then HTML special chars pass in the title.
‫And then instead of that we'll use the double double question mark.
‫So let's do that.
‫Place that double question mark and that should do the same thing.
‫So if I reload and continue, we get the same thing.
‫It just prints it on the page.
‫It looks a little cleaner though.
‫All right.
‫So let's now display the listing I'm going to put it down here.
‫So let's see.
‫We'll come down to where it says display submitted data.
‫And we're just going to add some HTML with some tailwind classes here.
‫So let's do a div with the class of let's say MT dash six.
‫I'm going to do p.
‫So padding four we'll give it a border.
‫Let's do rounded and a BG gray 200.
‫So that's our container div.
‫Then we'll have an h two.
‫And let's do a text dash lg say font dash semibold.
‫And let's say, submitted.
‫Um.
‫Submitted job listing.
‫Oops.
‫All right.
‫So under that H two we'll have a paragraph.
‫And in that paragraph we'll do a strong tag and say title.
‫And then let's do we can use our shorthand syntax and just output title.
‫And we'll do the same for description.
‫So I'm just going to copy this.
‫Copy that down.
‫And let's say description.
‫Change that to description.
‫All right.
‫So now I'm going to save it and I'm not going to reload.
‫I'm going to go just go to a fresh page and you'll see undefined.
‫Undefined variable title undefined variable.
‫Description.
‫So the reason that's happening is because we're setting the variable in the if statement.
‫So it's trying to load those variables before we actually check this if statement.
‫So one thing we could do is right above the if statement is just initially set the title to an empty
‫string and initially set the description.
‫To an empty string, and if I reload, then we're not going to see that.
‫We're not going to see that error.
‫Now, another thing I'd like is to not even show this if we haven't submitted it yet.
‫So what we can do is add another.
‫Add a third variable up here.
‫And we'll call that submitted.
‫And we're going to set that initially, set that to false.
‫And then what we can do is once everything is submitted, we don't want this echo here anymore.
‫We'll get rid of that.
‫Then we can set submitted.
‫Set that to true okay.
‫And then down here we can then check to see if it's been submitted before we display any of this.
‫So this HTML that we added let's add a PHP if.
‫And we're going to say if.
‫Submitted.
‫And then we'll come down here to the end, which is, I believe, this div right here.
‫And let's just do PHP and if.
‫Save that reload.
‫And now nothing's going to show down there if it hasn't been submitted yet.
‫All right now.
‫Let's actually submit something.
‫So we'll say listing one.
‫This is a listing.
‫Great listing.
‫And there we go.
‫We get submitted.
‫Job listing title listing one description.
‫This is a listing.
‫Now, I didn't set it up so that you could add multiple.
‫Like if I put something else in here, it's just going to replace what's already there.
‫We're not looping through or anything.
‫We're not creating a new array, anything like that.
‫The main point of this video is to show you how we get data from the from the inputs using the post
‫Superglobal.
‫Okay.
‫And again, if you're going to print stuff on the screen, you want to make sure it's secure.
‫You want to make sure that they can't execute JavaScript and do some cross-site scripting hacks to your
‫website.
‫All right.
‫So in the next video we're going to look at the request superglobal, which I wouldn't recommend using,
‫but I still want to show you it.