Actions

Difference between revisions of "Checkboxes form field type"

From Joomla! Documentation

(Created page with "The '''checkboxes''' form field type provides a multiple checkboxes. Unlike most used standard form field types like '''textfield''' or '''checkbox'...")
 
(Added details via an example.)
Line 1: Line 1:
The '''checkboxes''' form field type provides a multiple checkboxes. Unlike most used [[Standard form field types|standard form field types]] like '''textfield''' or '''checkbox''', this field is not "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 database.
+
The '''checkboxes''' form field type provides a set of checkboxes.  
 +
Note: unlike most [[Standard form field types|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:
 +
<source lang="xml">
 +
<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>
 +
</source>
 +
The set of checkboxes can be generated in your form with a single statement like this:
 +
<source lang="php"><?php echo $this->form->getInput('toppings'); ?></source>
 +
This will generate the following HTML, which can be styled using CSS:
 +
<source lang="html4strict">
 +
<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>
 +
</source>
 +
If the user checks the second and fourth item and submits the form, the Joomla server will provide the following result:
 +
<source lang="php">
 +
print_r(JRequest::getVar('jform')['toppings']) =>
 +
 
 +
Array
 +
(
 +
    [0] => chor
 +
    [1] => mush
 +
)
 +
</source>

Revision as of 10:05, 27 December 2012

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
)