C

Checkboxes and Radio Buttons

phpinfo Mo., 08. Januar, 2007 um 18:01 #1

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:1]<!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> [/code:1]

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.

[php:1]
<?
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>";
}
}
?>
[/php:1]

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

[php:1]if (!isset($_POST['sports'])) { [/php:1]

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.



C&M distanziert sich konkret und ausdrücklich vom Inhalt dieses Postings.
Der Ersteller des Postings haftet für seine Äußerungen.
Inhalte, die nicht den Forumsregeln entsprechen sind bitte vom Leser zu melden ...



Top