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.

I can't save the checkbox info (true or false) in a SafeCracker form.

April 13, 2012 5:49pm

Subscribe [2]
  • #1 / Apr 13, 2012 5:49pm

    jecampos

    10 posts

    Hello

    I am trying to save data from a SafeCraker form to a channel and actually I am able to save most of the data but checkbox field data.

    The code I am using is like this:

    -------------------
    {exp:safecracker channel="contacto" return="contacto/confirmacion" preserve_checkboxes="yes" rules:contacto_email="valid_email"}
    .
    .
    .     
    
    {options:contacto_recibir_correos}
    <input type="checkbox" class="checkbox" value="{contacto_recibir_correos}" name="{contacto_recibir_correos}[]" id="{contacto_recibir_correos}" checked />
    <label>{option_value}</label>
    {/options:contacto_recibir_correos}
    
    .
    .
    .
         
    {/exp:safecracker}
    -------------------

    Any idea what I am doing wrong?

    Thanks in advance

    Jhon E.

  • #2 / Apr 16, 2012 1:20pm

    Dan Decker

    7338 posts

    Hi jhonfields,

    Indeed!

    Inside the options loop, you need to use the available variable names, like so:

    {options:contacto_recibir_correos}
    <input type="checkbox" class="checkbox" value="{option_value}" name="{option_name}[]" id="{option_name}"{selected} />
    <label>{option_value}</label>
    {/options:contacto_recibir_correos}

    That should get you on the right track!

    Cheers,

  • #3 / Apr 16, 2012 6:18pm

    jecampos

    10 posts

    Hello Dan

    Tanks for your help. unfortunately the options you suggested does not solve out my problem, I am still unable to save the “selected or unselected” checkbox option in my channel.

    This is the URL of the form. http://198.170.128.123/contacto/

    {options:contacto_recibir_correos}<input type="checkbox" class="checkbox" value="{option_value}" name="{option_name}[]" id="{option_name}" {selected} /><label>{option_value}</label>{/options:contacto_recibir_correos}

    Thanks in advance.

    Jhon E.

  • #4 / Apr 18, 2012 5:11pm

    Shane Eckert

    7174 posts

    Hey jhonfields,

    Can I see the form in context?

    Are you enclosing these in any logic? See here.

    Cheers,

  • #5 / Apr 18, 2012 8:21pm

    jecampos

    10 posts

    Hello Shane

    Off course Shane, this is the complete form code:

    <div class="content">
       <div class="contact-form">
        <h3>Forma de contacto</h3>
    <p>    <div class="image"><br />
         {site_url}css/images/contact-image.jpg<br />
        </div><br />
        <form id="comment-form" action="" method="post"><br />
        {exp:safecracker channel="contacto" return="contacto/confirmacion" preserve_checkboxes="yes" rules:contacto_email="valid_email"}<br />
         <label>nombre</label><br />
         <input type="text" class="field" id="nombre" name="title" value="{title}" /></p><br />
         <label>email</label><br />
         <input type="text" class="field" id="email" name="contacto_email" value="{contacto_email}" /></p><br />
         <br />
         <textarea class="field" name="contacto_comentario" id="{contacto_comentario}" title="su pregunta o comentario">su pregunta o comentario</textarea><br />
          <label>Seleccione el tipo de pregunta o comentario:</label><br />
          <select class="field" name="contacto_tipo" id="contacto_tipo"><br />
             {options:contacto_tipo}<br />
             <option value="{option_value}">{option_name}</option><br />
             {/options:contacto_tipo}<br />
             </select><br />
         </p>
    
    <p>     {options:contacto_recibir_correos}<input type="checkbox" class="checkbox" value="{option_value}" name="{option_name}[]" id="{option_name}" {selected} /><label>{option_value}</label>{/options:contacto_recibir_correos}<br />
         <br />
         {if captcha}<br />
               <label for="captcha">Por favor ingrese la palabra de la siguiente imagen: </label></p>
    
    <p>           {captcha}</p>
    
    <p>           <input type="text" name="captcha" value="{captcha_word}" maxlength="20" class="field">/p><br />
         {/if}<br />
         <input type="submit" class="submit" value="enviar" /></p><br />
         <div class="cl"> </div><br />
        {/exp:safecracker}<br />
        </form><br />
       </div> <br />
      </div>

    Thanks again

    Jhon E.

  • #6 / Apr 19, 2012 4:38pm

    Shane Eckert

    7174 posts

    Hi Jhon,

    What if you follow this form?

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

    Are you using custom fields per the directions here?

    Thanks,
    Shane

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

ExpressionEngine News!

#eecms, #events, #releases