<p>You can pull an array of all the options for a checkbox field by calling <code>getOptions()</code>:</p>

<pre><code>$options = $entry-&gt;storyCategories-&gt;getOptions();
</code></pre>

<p>Each option will be an object with three properties, <code>label</code>, <code>value</code> and <code>selected</code>. To get all selected options for a field, you can loop through the options and test for the <code>selected</code> property:</p>

<pre><code>$options = $entry-&gt;storyCategories-&gt;getOptions();
$selectedOptions = array();
foreach ( $options as $option ) {
    if ( $option-&gt;selected ) {
        $selectedOptions[] = $option;
    }
}
</code></pre>

<p><em>Note that this exact code will work for Dropdown, Multi-select and Radio button fields as well.</em></p>

CC BY-SA 3.0
2015-04-07T00:36:47.663Z
A quick followup question. If I were to make a change to the selected value and push it back into craft via plugin logic, am I storing a json object?
David A McInnis
CC BY-SA 3.0
2015-04-07T07:48:29.763Z
@DavidAaronMcInnis The values are technically stored in the database as a serialized array (see the column ```field_storyCategories``` in the database table ```craft_content```, but as long as you use the [EntriesService](http://buildwithcraft.com/docs/plugins/working-with-elements#saving-elements) there's no JSON involved in the transport, no. If you have any other questions regarding updating entries/fields etc. it would probably be best to create a new question :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-04-06T18:50:32.680Z
2015-04-06T18:57:51.997Z
2015-04-06T18:57:51.997Z
Mats Mikkel Rummelhoff
<p>I have a checkbox field that is populate with user choices.  I need to get the values they have selected from within a plugin. Here is my attempt. Not working.</p>

<pre><code>    $entry  = craft()-&gt;entries-&gt;getEntryById($entryId, $localeId = null);
    foreach ($entry-&gt;storyCategories-&gt;options as $option) {
     var_dump($option);
    }
</code></pre>

<p>Can somebody please show me how to get these selected values?</p>

<plugin-development><checkboxes>
Retrieving Checkbox Values from Plugin
# Retrieving Checkbox Values from Plugin

(Asked by: username_0 on 2015-04-06)

I have a checkbox field that is populate with user choices.  I need to get the values they have selected from within a plugin. Here is my attempt. Not working.
    $entry  = craft()->entries->getEntryById($entryId, $localeId = null);
    foreach ($entry->storyCategories->options as $option) {
     var_dump($option);
    }

Can somebody please show me how to get these selected values?

**Tags:** plugin-development, checkboxes

---

## 1 answers

### Answer by username_1 on 2015-04-06. Score: 6 | Accepted answer

You can pull an array of all the options for a checkbox field by calling getOptions():
$options = $entry->storyCategories->getOptions();

Each option will be an object with three properties, label, value and selected. To get all selected options for a field, you can loop through the options and test for the selected property:
$options = $entry->storyCategories->getOptions();
$selectedOptions = array();
foreach ( $options as $option ) {
    if ( $option->selected ) {
        $selectedOptions[] = $option;
    }
}

Note that this exact code will work for Dropdown, Multi-select and Radio button fields as well.

> **Comments:**
> **username_0**: A quick followup question. If I were to make a change to the selected value and push it back into craft via plugin logic, am I storing a json object?  
> **username_1**: @DavidAaronMcInnis The values are technically stored in the database as a serialized array (see the column ```field_storyCategories``` in the database table ```craft_content```, but as long as you use the [EntriesService](http://buildwithcraft.com/docs/plugins/working-with-elements#saving-elements) there's no JSON involved in the transport, no. If you have any other questions regarding updating entries/fields etc. it would probably be best to create a new question :)  

---

