Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the most complex skills you need to learn to work with data entry forms is how to deal with multiple choice questions in a form that are represented by multiple check box controls. A single check box control is dealt with as a yes or no question. If the check box is selected, that means the user has selected the option, and if it's deselected, that means no. But with multiple check box selections to a single question, one very common way of dealing with it is to give all the check boxes the same name, and then to handle the results on the server using a little bit of PHP code.
I'll demonstrate how to do this with a version of my data entry form called join4.php, in the explorers folder. The first step is to make sure that all of your check boxes are named the same, and that they have a little bit of extra notation at the end of the name that turns them from a simple variable into something called an array. An array is an ordered collection of data. For example, this check box with a value of Backpack Cal has a name of interested, and if I look at the properties for the other check boxes in this set, you'll see they all have this same name.
In order to deal with them in PHP, change the name of the check box and add a beginning and ending bracket - that's the square bracket character, not the curly brace - and do the same thing for each of these check boxes. I'll click on each one in turn and type in the beginning and ending bracket, and then I'll be ready to do a little bit of hand coding in PHP. The actual processing of the PHP code is not something that Dreamweaver will help you with.
It's one of those circumstances where you just have to code it yourself. I have nine check boxes, they all have the same name, and they all have the beginning and ending brackets at the end of the name, indicating that they will be an array of values. Also notice that each of the check boxes has a unique, checked value. Let's take a look at how that is reflected in the actual HTML code. I'll go to Code View and show you that each of the check boxes is an input control. It has the name that I've assigned of interested with the array notation.
The type is check box. The ID is the same as the name right now, but that's something you might want to change later. Then the value for each of the check boxes is unique. Now when the user submits the form, we're going to be submitting the form back to itself. To make that happen, I've set the Action attribute using a PHP expression of _server PHP_self. That means the current page as it's seen by the browser.
So now go to the top of the code. Whenever you place code in a PHP page that already has a database connection include, place the custom PHP code after that. That will ensure that Dreamweaver can still find the connection for further work with databases. After the require_once command for the database connection, I'll create a new PHP code block. Then within the code block, the first question I'll ask is, has the form been submitted? The button in the form has a name of Submit, and that's going to create a new post variable named submit when the form is submitted.
So I'm going to use a PHP function called isset. I'll type the first few characters of the function, iss, and press Ctrl+Space, and there's my function, isset. Does the submit variable in the post-array exist? That code will look like this, $_POST, with post in all uppercase, then bracket, quote, submit, close quote, close bracket, close paren and close paren.
So, if this condition is true, that means the form has been submitted. I'll add a pair of curly braces where I can place code that I want it to execute only in that condition. Now, I'll ask the question, has the user checked any of the check boxes? Once again, I'll use the isset function. If the user checks at least one check box, there will be a post variable with the name of the check box, and it will be an array. If they haven't checked any of the check boxes, than that variable won't exist at all.
So, once again, I'll use an if command, and I'll call the isset function, and this time, I'll look for the variable 'interested.' Notice in this circumstance, I don't put in the opening and closing bracket. I only put that into the HTML code. When the user selects one or more items though, this variable will show up as an array. Once again, I'll add a pair of braces, and I'll also add an else section for the alternative state.
I need to make sure that I've added the extra parenthesis at the end of the line there, and that'll fix my syntax error. In the condition where the user has checked at least one of the check boxes, I'm going to transform the array - which is complex object - into a comma delimited string. For that, I'll use the PHP implode function. It looks like this. I'm going to create a variable called strInterested. It'll be a string values, so I'm prefixing it with str, and I'm going to get this value using the implode function, which looks like this.
You pass two values into implode. The first value, or argument, is called the glue. It's a character, or set of characters, that's going to separate the values in the array from each other. I'll type in ",". You can use either single or double quotes in this circumstance, and then I'll pass in, as the second argument, $_POST, bracket, quote, interested. This transforms the complex array into a string that has commas between the values in the array.
In the circumstance where the user hasn't selected any of the check boxes, I'll explicitly set strInterested to a blank string. So now, no matter what the user does with those check boxes, the strInterested variable will exist. I'm just going to add a little bit of debugging code. After the conditional clause, I'll use the echo command. I'll start with a literal string of "interested in," and then I'll append to that strInterested.
The dot is used to append one value to another. Then because I'm only debugging at this point - I'm not actually doing anything with the values - I'll call the PHP exit function, which terminates the processing of the current page and will simply output to the browser whatever I've echoed in this function. Now I'm ready to test my form. I'll save my changes and preview the page in an external browser, making sure that I copy the file over to my server directory. I have to fill in some minimal information in this form to satisfy its client-side validation rules, so I'll type into the first name Joey, last name will be Smith. The e-mail will be firstname.lastname@example.org.
I also need to fill in password information, because I have validation rules there. Then I'll go down to the Tool Profile section, and I'll select some random check boxes. I'll click the Join! button, and there's the result, a comma delimited list of the values that the user has selected. So that's how you prepare a form with multiple check boxes and then handle their selections on the server, using a little bit of custom PHP code.
This is an example of a circumstance where Dreamweaver can't actually generate the code for you, and so understanding how to create the code in PHP is an invaluable skill.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 87111 Viewers
80 Video lessons · 136229 Viewers
59 Video lessons · 54906 Viewers
52 Video lessons · 68785 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.