Retaining State of Radio Buttons and Checkboxes - CodeIgniter 3

I am finding the documentation and examples that I’ve Googled about this topic confusing. I can’t figure out the proper way to use set_radio() and set_checkbox to keep the values of radio buttons and checkboxes after form submission. Here is a portion of my form:

<div class="form-control">
     Client has read and understood the confidentiality guidelines.
     <?php 
         $data = array(
             'name'  => 'guidelines',
             'id' => 'signed_yes',
             'value' => 'yes',
             'style' => 'margin-left: 20px;'
             );
        echo form_radio($data); 
   ?>
   <?php echo form_label('Yes', 'signed_yes'); ?>
   <?php 
        $data = array(
             'name'  => 'guidelines',
             'id' => 'signed_no',
             'value' => 'no',
             'checked' => TRUE,   // default value
             'style' => 'margin-left: 20px;'
            );
       echo form_radio($data); 
   ?>
   <?php echo form_label('No', 'signed_no'); ?>
   </div><!-- end of .form-control -->
   <div class="form-control">
        A signed confidentiality guideline is on file for this client.
        <?php 
             $data = array(
                 'name'  => 'guidelines_signed',
                 'value' => 'yes',
                 'style' => 'margin-left: 20px;'
                 );
            echo form_checkbox($data)); 
       ?>
   </div><!-- end of .form-control --> 

How do I use set_radio() or set_checkbox() in this code? Nothing I try seems to work.

Why is nobody responding to this question?

Hi @WebMachine,

Sorry, I did spot the thread when you originally posted, but as I don’t have any experience with CodeIgniter I thought I’d hang back to see if anyone else could help and subsequently forgot all about it!

Looking through the docs, it seems that CI has two helpers for checkboxes: form_checkbox() for creating checkbox inputs, and set_checkbox() for repopulating their values after submission.

I think what you need to do is use both together:

echo form_checkbox(array(
    'name'    => 'guidelines_signed',
    'value'   => 'yes',
    'style'   => 'margin-left: 20px;',
    'checked' => set_checkbox('guidelines_signed', 'yes')
));
1 Like

Thanks. I got it to work with the checkbox, but no luck with radio buttons. And when I Google the issue, it seems like this problem is fairly common, but no-one is coming up with solutions other than not using the CodeIgniter form helper for radio buttons. I finally gave up yesterday and switched to pure html and php for just my radio buttons, because my deadline is fast approaching.

BUT, I hate loose ends and still would like to know how to make it work using the CodeIgniter form helper way of doing things.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.