print checkbox(Looking at the output, we see that this creates a checkbox with the name more_info and the value (if checked) yes. It is checked by default with checked="checked" and the label that surrounds it acts as the text informing the user of the checkboxes function:
-name => 'more_info',
-value => 'yes',
-selected => 1,
-label => 'Would you like more info?');
<label><input type="checkbox" name="more_info" value="yes" checked="checked" />Would you like more info?</label>Simple enough, right? But what happens when we want multiple boxes, say for a set of related options? Here's an example where we're setting a group of color options:
my %labels = (And the output is:
'red' => 'A Red One',
'green' => 'A Green One',
'blue' => 'A Blue One',
'yellow' => 'A Yellow One' );
print checkbox_group(
-name => 'color_choices',
-values => ['red', 'green', 'blue', 'yellow'],
-default => ['red', 'blue'],
-labels => \%labels
);
<label><input type="checkbox" name="color_choices" value="red" checked="checked" />A Red One</label>So if we look at the differences, the first and most obvious is that we're setting up a hash with all the label values in it. This is the text that will appear alongside the checkbox describing what it does. That hash gets passed on via a pointer in the -labels option.
<label><input type="checkbox" name="color_choices" value="green" />A Green One</label>
<label><input type="checkbox" name="color_choices" value="blue" checked="checked" />A Blue One</label>
<label><input type="checkbox" name="color_choices" value="yellow" />A Yellow One</label>
Next up we have the same -name attribute, and a slightly different -values attribute. Note the S on the end of the value, and the fact that it's now an array of values rather than a single value. Finally we set the -default checked values (in this case red and blue) and pass the -labels pointer.
