Skip to content
Advertisement

How do you set a CodeIgniter radio button, before it’s in the $_POST?

So in the CodeIgniter form helper, (https://www.codeigniter.com/userguide3/helpers/form_helper.html) you have this function:

set_radio()

Permits you to display radio buttons in the state they were submitted. This function is identical to the set_checkbox() function above.

<input type="radio" name="myradio" value="1" <?php echo set_radio('myradio', '1', TRUE); ?> />
<input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />

The problem is that I already have information BEFORE I present the form. As I understand it, this set_radio() function uses the data in the POST action to set the value when the form validation fails, and needs to be redone.

Does that make sense?

So if I haven’t done the first POST yet, the set_radio() doesn’t have any data to use to pre-set the radio button.

Advertisement

Answer

You can always modify $_POST variable, like this:

if (!isset($_POST['myradio'])) {
    $_POST['myradio'] = $myDefaultValue;
} 

Do this before you start working with form helper and you should be fine.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement