Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the previous movie, we learned to create and retreive the values from a web form, but we ran into an issue. When the form was submitted, our page worked just fine. But if the URL was loaded directly, instead of being submitted by the form, then the key values that our code was expecting to be in the post array weren't there. In this movie, we're going to talk about how you can detect whether or not a form has been submitted, so that you can handle this case appropriately. So, the problem that we have is that we're looking for this key, username, and it's not there. It doesn't exist in the post array, because the form wasn't posted. So therefore, it doesn't exist, and we're getting an error when we try and access it.
One simple solution to this would be to use is set. We remember how to do that, so we could do something like this. We could say if and then is set, no spaces. Dollar sign post and then username. So if that is set, then set username. And we can do the same thing for password. So now if we go back and try our page, we're just going to load it directly by hitting return and we still come up with an error. Why are we getting the error this time well this time it's different. This time it's undefined variable. That's because here we're trying to use it but if it's not set. Well this value doesn't get set so now this doesn't exist. So we solved one problem and created another problem at the same time. So we could do else dollar sign user name equals, and then let's do the same thing down here, else dollar sign password equals.
Now let's go back and load up our page this time, and now we've solved both problems. We're still getting this stray colon output, because it's outputting blank string, followed by a colon and a space, and then a blank string. But we've gotten rid of the errors by doing this. But we've written a whole lot of code here, just to handle this. Now, this incidentally is a good way to set default values. I'm going to go ahead and make a note here that default values. Because if you needed a default value for something, this is a good way to do it, to say If it's set, then set it to that value. If it's not set, set it to your default, and you would just provide the default value here. It doesn't have to be empty.
It could be my default value is Kevin. Maybe if you have a form that has a country field, you want to default to your current country, the United states or Canada or something like that. Now, If you do decide to go this way, I just want to show you that there is a better way to do that, and that is by using the ternary operator. Let me just close the PHP tags here. There we go. And let's open a new one. And now those are separated. And let me just show you what a ternary operator looks like. We didn't talk about this back when we talked about if statements, conditionals. It's just a special format for doing the if statements, and it has a Boolean test, followed by a question mark, then the value if the test is true. Then a colon and then the value of the test is false. So, you can see that this is a lot more efficient than writing all of this, but it does the exact same thing.
So, it's just a more compact way of writing it. If this is set, then set it to this, if not, set it to that. So, I want you to see that because that's a little more efficient. I think, a nicer way to do that. But, I think there's an even better way to do it than that, in this case. A way to detect whether the form has been submitted or not. And the way to do that, will put an if statement, is set. Just like we just did. And we'll look in that post array. But were going to look for submit. And then, for now, let's just do echo, form was submitted.
So, what we're doing here. Let's go back and remind ourselves what that looks like. Let's go to the form, and let's submit it. I'll click submit. Notice that there's this other value here called submit. Doesn't matter what all of the form is. I have something here called submit. And that's what I'm checking. Form was submitted. So it did, in fact, say that it was submitted. If I come here, and I do it this way. Now the form was not submitted. All of my default values also kicked in. That's beside the point. The thing that I want you to see is that this form was submitted whether it shows or not. So what's required to make this work is this.
Name equals and then something. I just use submit, that's the easiest thing. But you need to have some name attribute there. because that's going to make sure that in any time that the submit button is clicked, it's value is included in that array. If we take that away. Let's try that. Let's go back and we'll reload this page. And I'll just type in Kevin, password and Submit. Now it's not there. So that's the key thing. You have to have this name equal submit for this trick to work. So what does that gain me? Why do I want to do that? Well now, I have one way to check right off the bat whether or not this has been submitted.
Before I do any of these other tests, I can take all of these. Let me just cut it and let's paste it in here, and the ternary one. I'll go ahead and grab as well, just so that they're all together, and get rid of that. Okay? You see the difference? So now It's going to check and see whether the form was submitted, and if it is, it's going to do all of this processing. If not, it skips right past it. Now, it'll give me an error if I try and load this, because this is outside of that, and I don't have a value for username and password if I skip everything.
But I could easily fix that. Else and then set the default values here, username equals and password equals. That will then work just fine. Let's come back over here, form processing, reload the page, none of that takes place, these are the values that kick in. And if you want to prove to yourself you can put in Bob here Lets just reload the page you see that's the value being set. So this checks to see whether not post variables submit a set. And then we know it was posted to us. If it wasn't then it's a get request then we can handle the page differently based on whether it's a get request or a post request.
What's nice about this, is that instead of using two pages to do our form submission. One that has the form and one that receives the form, we can actually use a single page to both have our form and perform our form processing. Let's look at that next.
Get unlimited access to all courses for just $25/month.Become a member