Actions

Difference between revisions of "Checkboxes form field type"

From Joomla! Documentation

(Added details via an example.)
 
Line 38: Line 38:
 
)
 
)
 
</source>
 
</source>
 +
 +
[[Category:Standard form field types]]

Latest revision as of 18:42, 1 July 2013

The checkboxes form field type provides a set of checkboxes. Note: unlike most standard form field types, such as textfield or checkbox, this field is not an "out of the box" solution. It will create checkboxes for you, and submit their values in form of an array, but it will not store them in the database.

Example:

<field name="toppings" type="checkboxes">
    <option value="anch">Anchovies</option>
    <option value="chor">Chorizo</option>
    <option value="on">Onions</option>
    <option value="mush">Mushrooms</option>
</field>

The set of checkboxes can be generated in your form with a single statement like this:

<?php echo $this->form->getInput('toppings'); ?>

This will generate the following HTML, which can be styled using CSS:

<fieldset id="jform_toppings" class="checkboxes">
        <ul>
                <li><input type="checkbox" id="jform_toppings0"
                        name="jform[toppings][]" value="anch" /><label for="jform_toppings0">Anchovies</label></li>
                <li><input type="checkbox" id="jform_toppings1"
                        name="jform[toppings][]" value="chor" /><label for="jform_toppings1">Chorizo</label></li>
                <li><input type="checkbox" id="jform_toppings2"
                        name="jform[toppings][]" value="on" /><label for="jform_toppings2">Onions</label></li>
                <li><input type="checkbox" id="jform_toppings3"
                        name="jform[toppings][]" value="mush" /><label for="jform_toppings3">Mushrooms</label></li>
        </ul>
</fieldset>

If the user checks the second and fourth item and submits the form, the Joomla server will provide the following result:

print_r(JRequest::getVar('jform')['toppings']) =>
 
Array
(
    [0] => chor
    [1] => mush
)