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.

Advice on how to use value of checkbox field from safecracker form

August 17, 2011 7:34am

Subscribe [3]
  • #1 / Aug 17, 2011 7:34am

    Mediakitchen

    261 posts

    I am using safecracker to create a form for advertisers to place adverts on my site.

    I want to ask them if they would like to include their telephone number and email address on their advert so have included the following checkbox custom field:

    contact_preferences

    This field has the following 2 multi-select options

    Display my email address
    Display my telephone number

    I display this checkbox as follows:

    {custom_fields}
    
         {if checkbox}
    
        {options}
        <label class="checkbox"><input type="checkbox" id="{field_name}" name="{field_name}[]" value="{option_value}"{checked} /> {option_value} </label>
    
        {/options}
         {/if}
    
    {/custom_fields}

    When I submit my form, I can see via the control panel that it works.

    My question is how would I include an if statement in my code to display the advert based on the value of this custom field?

    Here is some psuedo code to try and show what I need

    {exp:channel:entries channel="adverts" show_empty="yes"  limit="1"}
    
    if {contact_preferences["Display my email address"] == true}
    
         {contact_email}
    
    end if
    
    if {contact_preferences["Display my telephone number"] == true}
    
         {contact_telephone}
    
    end if

    I see if I just display

    {contact_preferences}

    within the channel entries tag, I get the following if I had checked both checkboxes:

    Display my email address, Display my telephone number

     

    Thanks in advance for any help

    Paul

  • #2 / Aug 17, 2011 8:52am

    im having the same issue. Checkbox is not working. I have no idea what is wrong, and It very much looks like it is indeed a bug in EE2.

    EE2.2.1
    Dmitry

  • #3 / Aug 17, 2011 9:20am

    Mediakitchen

    261 posts

    I don’t think my issue is a bug as I am getting the values set correctly. It is just because my checkbox as multiple values, the results are displayed as a comma delimited string. I am guessing I need to somehow extract the separate elements.

    I am however trying out an alternative approach using Radio buttons instead which appears to eliminate my issue.

    @Dmitri - when you say the checkbox is not working, what errors are you getting?

  • #4 / Aug 17, 2011 9:50am

    i also failed to figure out the way how it works within conditional statement. (i.e like in your psuedo code)

    i meant this is a bug bcs it behaves very strange, in my case checkbox won’t save it’s value after submitting it in safecracker. what version of EE you’re running? can you check this also (i.e saving ability) ?

  • #5 / Aug 17, 2011 9:52am

    but you’re right there is work around, i just used PT fields for that purpose.

  • #6 / Aug 17, 2011 7:28pm

    Dan Decker

    7338 posts

    Hi Paul,

    I think you might get the results you want if you use your checkbox field as a tag pair. For example:

    {checkbox_field}
      {if item == “My special Value”}
      Conditional Content
      {/if}
    {/checkbox_field}

    Can you adapt that pseudo code to your template and let us know if that gets you where you want to be?

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases