Checkboxes and Radio Buttons

Tutorials on PHP, databases and other aspects of web development. Before posting a question, check in here to see whether there's a tutorial that covers your problem.

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Checkboxes and Radio Buttons

Post by JayBird »

Checkboxes and Radio Buttons

Having trouble with you checkboxes and radio buttons. Here is you one stop answer shop for everything you ever wanted to know about them and how to use them in conjunction with PHP (obviously)

Difference between the two
Just so that there is no confusion, i will quickly tell you what the difference is and when to use each.

Checkboxes are mainly use when presenting the user with a number of options, of which, they can select more than one option. For example, a form asking a user what topics they are interested in, obvioulsy the user may be interested in more than one topic.

Radio buttons are generally used when presenting the user with an "either/or" option. The user is presented with two or more options, of which, the user can only select one option. For example, a form asking the user which method of payment they want to use. The user can only select one.


Checkboxes

Okay, lets construct a simple form asking the user what sports they like. There will be multiple options of which the user can select none, some or all the options.

Code: Select all

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<form name="form1" method="post" action="results.php">
<table width="300"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><input type="checkbox" name="sports[]" value="Football"></td>
    <td>Football</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Rugby"></td>
    <td>Rugby</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Cricket"></td>
    <td>Cricket</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Formula 1"></td>
    <td>Formula 1 </td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Tennis"></td>
    <td>Tennis</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Golf"></td>
    <td>Golf</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="sports[]" value="Darts"></td>
    <td>Darts</td>
  </tr>
</table>
<p>
  <input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
 
Notice that each checkbox has been given exactly the same name? The reason for which will become apparent in a moment.

Now, we need some PHP to process the form. We are just going to output the options the users selected, but this should give you the concept of how this works so you an adapt it to insert the values in a Database, or anything else you want to do with them.

Code: Select all

 
 
if (!isset($_POST['sports'])) {
    echo "You did not select any sports, you really should do some exercise!";
} else {
    echo "You selected the following sport(s):<br>"
    foreach ($_POST['sports'] as $selected_sport) {
        echo $selected_sport."<br>";
    }
}
 
Can you see why we named the checkboxes as we did yet? Naming all the checkboxes sports[] instructs PHP to construct an array called $sports containing all selected values.

Easy eh?

So lets me just explain the PHP code a little.

A common mistake i see people making when using checkboxes is that they do not allow for someone not selecting anything. Then, when they run their script they get an error saying Undefined index.

This is because, if the user decides not to select any of the options, the array $sports will not be created at all - not even empty.

This is why, in the PHP snippet above, we have used

Code: Select all

 
if (!isset($_POST['sports'])) {
 
This checks to see if the user actually selected anything. If they didn't, we can output the message "You did not select any sports, you really should do some exercise!".

If the user did make some selections, we jump to the next part of the code, and display all the options they selected. We do this using the foreach function. We itterate through the $sports array, outputting each value to the broswer.
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

Am I being watched? I was just going to look for some info on creating arrays with checkboxes no more than half an hour ago...***Shudder***

Thanks tho, that's just what I needed to know! :D
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

No probs, i was just tidying my desktop and found this that i wrote a while ago.

Thought i would re-post it cos all the other tutorial went missing.

Mark
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i wrote something similar for more info on it. It uses checkboxes, can be found under code snipplets
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

What happened to the thread contents?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The usual. Old new php tags mess with the new php tags.


Edit: there, fixed it.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Sweet!
ajaygolani
Forum Newbie
Posts: 1
Joined: Sat Jan 01, 2011 10:26 pm

Re: Checkboxes and Radio Buttons

Post by ajaygolani »

Hi,
Very nicely explained. Thanks
benji2010
Forum Newbie
Posts: 12
Joined: Fri Apr 01, 2011 1:41 am

Re: Checkboxes and Radio Buttons

Post by benji2010 »

Hey Mark,

Many thanks for this, it is really good. If possible would you be kind enough to post something similar for drop down boxes and colours? I am creating an ecommerce site and need drop down options for colours and sizes?

Please help me,

Thanks,

Ben.
putur12
Forum Newbie
Posts: 4
Joined: Wed Apr 20, 2011 6:24 am

Re: Checkboxes and Radio Buttons

Post by putur12 »

yes, please for drop down boxes.
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re:

Post by Live24x7 »

Skittlewidth wrote:Am I being watched? I was just going to look for some info on creating arrays with checkboxes no more than half an hour ago...***Shudder*** Thanks tho, that's just what I needed to know! :D

Often I have similar feelings - I bump upon thngs which i need and it feels as if it is a part of some cosmic plan ;)

Thanks for the explnation - will need it in the next few days
Post Reply