Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,974 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Installing XAMPP on a Windows system
- Installing MAMP on a Mac system
- Using admin consoles
- Creating PHP pages
- Commenting code
- Working with web forms
- Adding custom validation
- Using redirects
- Dealing with email issues
Skill Level Intermediate
I'm now going to talk about the difference between some of these form fields and things you need to keep in mind as a developer when you're working in the various types of form fields. So we'll take a look at what the different types are that we have on our form. Right now, within this form, we have text fields, we have a drop-down menu, we have radio buttons, check boxes, and a text area.
So we do have quite a few forum field types. Now, those form field types, it makes a difference when you're trying to work with them on the Action page. If I put this in the browser, you'll see what I'm talking about. So I have my form, I'm not going to fill anything out. If I click Contact Us, we have a problem. So lets take care of this problem first. Now, notice our form fields, which are text fields, didn't have a problem. Our topic, let me move this over so you can see, our topic specifically is a drop down menu...
We didn't have a problem with that. Present customer, our radio buttons weren't a problem either. That's because of these different features as long as it's a single selection drop-down list, and I have something checked initially when I fill out the form. In other words, I have one of these radio buttons preselected for the user. In this example, I don't. But if I did, we would not have a problem with these four form fields. What we have a problem with is if the user doesn't choose a check box at all, that check box information does not get sent to the action page. So I don't have anything listed in Tour Date.
I'm getting an error. Let me fill it out with a Tour Date, and you'll be able to see that. So I will use the Populate Form Fields option. This is the Web Developer toolbar. If you're stepping into this movie and don't have this toolbar installed, so I will use, Form > Populate Form Fields, fills out all of the form fields for me. I'm going to uncheck these.
So I'll just send two, click Contact Us Notice tour dates shows up, but if I go back to the form and don't check anything we do not get the text, we get an error. The text is listed down below it, but here's our problem. So line 15 is what's causing the problem. What happens is this function we have set up in PHP is trying to perform or do a task and there is noting to do the task on.
So we can fix that fairly easily but we do have to do some coding ourselves in order to do that. So the coding we're going to do is on the Action page. And what we need to do at the top of the Action page is. We want to make sure that the user set up some information before we try and use the implode function on it. So, within our PHP color block, we're going to type out what is known as a conditional statement. And here is the basic skeleton of a conditional statement. It has the word if, it has parentheses, and then curly brackets. The first curly bracket goes to the left.
The little point in the middle of the bracket is going to the left-hand side and this one is going to the right side. And what we're going to do is put some information inside of here. We want to test to see if our tour date form field had been sold out. So if its not filled out then we're going to do something. So this means if something is not filled out in our form. So we use Not and the Is set function to set this up. So what are we going to test for? We're going to test for a variable coming from the form, so it's a post variable.
And the post variable we're going to use is Tour Date. Why did I choose that one? Well that is the one that could cause a problem. We're trying to use the implode function on it and it doesn't exist. So lets go ahead and add this, so post Tour Date. Notice that I no longer have syntax errors. So if tour date is not coming from our form, we're going to do something. And what we're going to do is $_post. We're going to take that Tour Date.
You can just copy it from up above if you'd rather not type. And we're going to assign it to array. So there's our code. If the user filled out the tour date, this will not run. So what happens is in an if statement, it's testing to see if this is true. So what is it we're testing? We're testing to see if the user didn't fill out any check boxes for tour date. If they didn't, we're going to create a variable and we're going to assign it to no values.
That will make sure that this exists before we try and do something with it. So this will prevent that error. Let's go back to the form. I'll put in in Firefox, only because I can use the Populate Form Fields. I will uncheck our check boxes now, because that's what we're testing for. Click Contact Us and we do not have a problem. You can see, it shows up empty. If we go back to our form and check them, and this is something to consider as you're moving into PHP or any server side language.
When you add something new, don't always assume that the old stuff is working that you had already tested. Always go back and test it as well. So I will test it with the check boxes as well. Not a problem. Without the check boxes, not a problem. So we're all set on our form. Tour dates is not going to cause us a problem. Any time you work in check boxes, it can become an issue, so it's important to plan for that in advance. Now, if we fill out our form here, what would happen if no radio buttons are selected? Radio buttons are not an issue for us so in this example we won't have to worry about those. If the user doesn't select them it's not a problem. We're not trying to perform something on that value. We just won't get a value printed.
So that is working with check boxes. Also how to test for our form variable, or any type of variable actually, to see if it exists before we try and do something. So you could put any variable in this area after is set. Whether it's a form variable or not. Whatever you're going to put inside those parentheses. It's going to test and see if it exists. If it doesn't exist. Because, we said, if it's not existing. We can put in here what to do if it doesn't exist. And what we're doing is creating that variable, and setting it to an array variable in PHP.
That way we know this is always going to exist. So, our implode function down below will work properly. So that is working with check boxes on a form.