<p>The first loop you're using loops over <code>entry.gistCategories.options</code>, which contains all the options available. However, in the second loop (when <code>entry.gistCategories|length !== 0</code>), you're only looping over the options that have been selected.</p>

<p>Try this:</p>

<pre><code>{% if entry.gistCategories | length == 0 %}
    &lt;ul&gt;
        {% for option in entry.gistCategories.options %}
            {# output checkboxes #}
        {% endfor %}
    &lt;/ul&gt;
{% else %}  
    &lt;ul&gt;
        {% for checkboxOption in entry.gistCategories.options %}
            {# output checkboxes #}
        {% endfor %}
    &lt;/ul&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-02-17T20:58:52.070Z
2015-02-17T20:58:52.070Z
Keith Bartholomew
<p>Since the form was already submitted, your code is only looping through the checkboxes that were checked.</p>

<p>Just add <code>.options</code> to your second <code>for</code> block like you have above:</p>

<pre><code>{% for checkboxOption in entry.gistCategories.options %}
</code></pre>

<p>That'll loop through all of 'em, checked or not.</p>

CC BY-SA 3.0
2015-02-17T20:56:17.500Z
2015-02-17T20:56:17.500Z
RitterKnight
<p>I have tried to follow the instructions listed <a href="http://buildwithcraft.com/docs/checkboxes-fields" rel="nofollow noreferrer">here for checkboxes on frontend forms</a> but have not been successful in implementation.</p>

<p>First, here is my code:</p>

<pre><code>&lt;form method="post" action="" accept-charset="UTF-8" enctype="multipart/form-data"&gt;
&lt;div class="form-group"&gt;
&lt;h3&gt;Push to GistServers for Gist {{ entry.id }}&lt;/h3&gt;
&lt;input type="hidden" name="action" value="gistClient/pushGistUnits"&gt;
&lt;input type="hidden" name="entryId" value="{{ entry.id }}"&gt;
&lt;p&gt;Select Gist Categories for Distribution.&lt;/p&gt;
{% if entry.gistCategories | length == 0 %}
    &lt;ul&gt;
        {% for option in entry.gistCategories.options %}
        &lt;input type="checkbox" value="{{ option.value }}" name="fields[gistCategories][]"&gt; {{ option.label }}&lt;br&gt;
        {% endfor %}
    &lt;/ul&gt;
{% else %}    
    &lt;ul&gt;
    {% for checkboxOption in entry.gistCategories %}
        {% set checked = false %}

        {% if checkboxOption.selected %}
            {% set checked = true %}

        {% endif %}

        {% if not checked %}
            &lt;input type="checkbox" value="{{ checkboxOption.value }}" name="fields[gistCategories][]" {% if checked %} checked="checked"{% endif %}&gt;{{ checkboxOption.label }}&lt;br&gt;
        {% endif %}

        &lt;input type="checkbox" value="{{ checkboxOption.value }}" name="fields[gistCategories][]" {% if checked %} checked="checked"{% endif %}&gt;{{ checkboxOption.label }}&lt;br&gt;
    {% endfor %}
    &lt;/ul&gt;
{%endif%}

{{ getCsrfInput() }}
{% if entry.socialPush == true %}
    {% set socialPush = "Push to GistServers"%}
    {% set socialPushBtn = "default"%}
{% else %}
    {% set socialPush = "Push to GistServers"%}
    {% set socialPushBtn = "success"%}
{% endif %}

&lt;button class="btn btn-{{socialPushBtn}}  btn-sm btn " style="margin:10px 15px 0 0;" type="submit" name="fields[gistDraft]" value="0"&gt;{{ socialPush }}&lt;/button&gt;
&lt;/div&gt; 
</code></pre>

<p></p>

<p>Now for screen caps showing behavior. </p>

<p>Before form submit</p>

<p><img src="https://i.sstatic.net/vaylr.png" alt="enter image description here"></p>

<p>CP View after form submit</p>

<p><img src="https://i.sstatic.net/vpFIq.png" alt="enter image description here"></p>

<p>Frontend form after submit.</p>

<p><img src="https://i.sstatic.net/XC7I6.png" alt="enter image description here"></p>

<p>Ideally, After I submit the frontend form my users would be able to see all checkboxes again in case they need to change the selections. I am at a loss and would appreciate some rescue here.</p>

<frontend><checkboxes>
Getting Checkboxes to work on Frontend Forms
# Getting Checkboxes to work on Frontend Forms

(Asked by: username_0 on 2015-02-17)

I have tried to follow the instructions listed here for checkboxes on frontend forms but have not been successful in implementation.
First, here is my code:


Push to GistServers for Gist {{ entry.id }}


Select Gist Categories for Distribution.
{% if entry.gistCategories | length == 0 %}
    
        {% for option in entry.gistCategories.options %}
         {{ option.label }}
        {% endfor %}
    
{% else %}    
    
    {% for checkboxOption in entry.gistCategories %}
        {% set checked = false %}

        {% if checkboxOption.selected %}
            {% set checked = true %}

        {% endif %}

        {% if not checked %}
            {{ checkboxOption.label }}
        {% endif %}

        {{ checkboxOption.label }}
    {% endfor %}
    
{%endif%}

{{ getCsrfInput() }}
{% if entry.socialPush == true %}
    {% set socialPush = "Push to GistServers"%}
    {% set socialPushBtn = "default"%}
{% else %}
    {% set socialPush = "Push to GistServers"%}
    {% set socialPushBtn = "success"%}
{% endif %}

{{ socialPush }}
 


Now for screen caps showing behavior. 
Before form submit

CP View after form submit

Frontend form after submit.

Ideally, After I submit the frontend form my users would be able to see all checkboxes again in case they need to change the selections. I am at a loss and would appreciate some rescue here.

**Tags:** frontend, checkboxes


---

## 2 answers

### Answer by username_1 on 2015-02-17. Score: 3 | Accepted answer

The first loop you're using loops over entry.gistCategories.options, which contains all the options available. However, in the second loop (when entry.gistCategories|length !== 0), you're only looping over the options that have been selected.
Try this:
{% if entry.gistCategories | length == 0 %}
    
        {% for option in entry.gistCategories.options %}
            {# output checkboxes #}
        {% endfor %}
    
{% else %}  
    
        {% for checkboxOption in entry.gistCategories.options %}
            {# output checkboxes #}
        {% endfor %}
    
{% endif %}


---

### Answer by username_2 on 2015-02-17. Score: 2

Since the form was already submitted, your code is only looping through the checkboxes that were checked.
Just add .options to your second for block like you have above:
{% for checkboxOption in entry.gistCategories.options %}

That'll loop through all of 'em, checked or not.


---

