ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

using “if” conditionals on checkboxes

June 14, 2012 12:34pm

Subscribe [3]
  • #1 / Jun 14, 2012 12:34pm

    77120

    15 posts

    im trying to make a if conditional on a checkbox with multple boxes
    but if theres more then one box selected it doenst work

    {if checkbox == "foo"}
    works
    {/if}
    {if checkbox == "foo, bar"}
    doenst work
    {/if}

    how can i get this to work?

  • #2 / Jun 14, 2012 1:05pm

    Rob Allen

    3108 posts

    How many values could your checkbox have?

    Normally I’d use a conditional for each possible value;

    {if checkbox_field == "Yes"}
    Answer yes
    {/if}
    
    {if checkbox_field == "No"}
    Answer no
    {/if}
    
    {if checkbox_field == ""}
    Not specified
    {/if}

     

  • #3 / Jun 14, 2012 1:16pm

    77120

    15 posts

    if two options are checked it must be a different answer than if just one of them is selected

    so with “foo, bar” i meant 2 different options being selected.

    i indeed didnt make that clear 😊

  • #4 / Jun 15, 2012 5:21am

    Rob Allen

    3108 posts

    Ok, maybe use the standard tag loop to output the checkbox data:

    {checkbox_field}
        {item}
    
    {/checkbox_field}

    That would output any value that has been checked.

    Alternatively you could use a conditional to only display certain item values:

    {checkbox_field}
        
      {if item == "Red"}
      {item}
      {/if]
    
    {/checkbox_field}


    Docs at http://ellislab.com/expressionengine/user-guide/modules/channel/custom_fields.html#checkbox-fields

  • #5 / Jun 15, 2012 6:09am

    77120

    15 posts

    ah yes thanks that did the trick, more easy then i thought it would be 😊

    i now can use the conditional as {if item == "Red, Blue"}

  • #6 / Jun 15, 2012 6:59am

    Rob Allen

    3108 posts

    ah yes thanks that did the trick, more easy then i thought it would be 😊

    i now can use the conditional as {if item == "Red, Blue"}

    Not that syntax, but could use a complex conditional:

    {if item == "red" OR item == "blue"}
    {item}
    {/if}

     

  • #7 / Jun 15, 2012 7:05am

    77120

    15 posts

    hmm it does work with the ,

    and otherwise i think it should be AND

    {if item == "red" AND item == "blue"}
    {item}
    {/if}

    which also works, thanks i will use that

     

  • #8 / Jun 15, 2012 7:09am

    Rob Allen

    3108 posts

    Does it work with comma separated values? Not seen that before 😊

    Good stuff - you could use AND or OR depending what you want to compare.

  • #9 / Jun 15, 2012 11:37am

    Shane Eckert

    7174 posts

    Hey 77120,

    I am glad to see that you and Rob were able to resolve your issue.

    If you need anything else, please just let me know by opening a new thread.

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases