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>
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>";
}
}
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'])) {
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.