2

I have 5 check-box. For each check-box, 5 radio buttons get generated. These radios value get saved to database. Now what I want is, when I select check-box, and if the selected check-box has some database value already saved in database, that radio button should show saved database value, mean to say, it should be shown selected. 5 check-box are like: 1. A 2. B 3. C 4. D 5. C

All check-box has radio options like: Male or Female

If(say) I choose "Male" for "A" and submit my form. when I again go to that form and choose "A", "male" radio button should be shown selected.

some code implementation:

foreach ($check-box-options as $key => $value) {

        $form['radio_'.$key] = array(
            '#type' => 'radios',
            '#title' => t('abc'),
            '#options' => $filter_radio_options,
            '#default_value'=> ???,
            '#states' => array(
                'visible' => array(
                    ':input[name="filter_criteria_options['.$key.']"]' => array('checked' => TRUE),
                ),
            ),
        );
}

$check-box-options is an array of check-box A,B,C,D,E. For each check-box, I have radios.

$serialized = unserialize($radio_buttons_database_values);

$serialized contains all selected radio button values. If I replace $check-box-options with $serialized, I got correct radio button selected as default, but if I loop $serialized as nested inside $check-box-options, it is throwing last selected value from database for every check-box. I am not getting the exact problem with my code or approach.

Can anyone please help me understanding the exact problem behind this?

  • Have you tried variable_get()? Can we see how you save these values? Is it a settings form? Is it a node? – leymannx Aug 24 '15 at 16:26
4

As the radios are separate field in itself, default value should be as easy as follow

foreach ($check-box-options as $key => $value) {
        //A. A function to query database to get default value
        $default_value = _my_module_helper_get_default_radio_option($key);

        //B. Or if its a system setting form, all values are inside the variables, in that case all you have to do is variable_get()
        $default_value = variable_get('radio_'.$key);

        // Depending on form type use option A or B.

        $form['radio_'.$key] = array(
            '#type' => 'radios',
            '#title' => t('abc'),
            '#options' => $filter_radio_options,
            '#default_value'=> $default_value,
            '#states' => array(
                'visible' => array(
                    ':input[name="filter_criteria_options['.$key.']"]' => array('checked' => TRUE),
                ),
            ),
        );
}

// In case option A is what you needed.
function _my_module_helper_get_default_radio_option($key) {
  return db_select('your_table','yt')
         ->fields('yt',array('field_name'))
         ->condition('yt.field_is',$key,'=')
         ->exectue()
         ->fetchField();

 }
  • function _my_module_helper_get_default_radio_option($key) { $get_defaults = db_select('your_table','yt') ->fields('yt',array('field_name')) ->condition('yt.field_is',$key,'=') ->exectue() ->fetchField(); return unserialize($get_defaults); } can I write this function this way? Will this also work? @Arpitr – Sugandh Khanna Aug 25 '15 at 6:04
  • if you are serializing the values before saving it to database, then yes. But I don't see a need for serializing at all. So the above function expects each radio field will have a field name and its value as an option in database, the reason we are doing a query inside a loop. – arpitr Aug 25 '15 at 6:19
  • and can you explain ->condition('yt.field_is',$key,'=')? what should be field_is here? – Sugandh Khanna Aug 25 '15 at 6:26
  • that was an example, should be replaced with the column name in your database table for holding radio field names – arpitr Aug 25 '15 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.