How do you set a CodeIgniter radio button, before

2019-08-28 23:12发布

问题:

So in the CodeIgniter form helper, (http://ellislab.com/codeigniter/user-guide/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.

回答1:

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.