Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a form that has 5 radio buttons. Two of those have optional additional parameters that the user can select. If radio 1 is selected, then only the optional items for that radio would make sense to be populated. Is there a way using the Forms API to accomplish this?

I have the following code. The Optionals Extra2 is the olnly select boxes that are shown. If I change the $form['mc']['cats'] to $form['mc']['cats2'] I can get it to display but it's not in the same fieldset as the rest.

$form['mc'] = array(
    '#type' => 'fieldset',
    '#title' => t('Membership Categories (select only one)'),
    );




$form['mc']['cats'] = array(
    '#type' => 'fieldset',
    '#title' => FALSE,
    '#prefix' => '<div style="background-color:red;">',
    '#suffix' => '</div>',
    );

$form['mc']['opts'] = array(
    '#type' => 'fieldset',
    '#title' => FALSE,
    );

$membership_cats = array(
    0 => t('Option 1'),
    1 => t('Option 2'),
    2 => t('Option 3'),
    3 => t('Option 4'),
    4 => t('Option 5'));


    $form['mc']['cats']['profile_memberlist']['field_membership_category']['#prefix'] = '<div style="background-color:salmon;float:left;width:50%;">';
    $form['mc']['cats']['profile_memberlist']['field_membership_category']['#suffix'] = '</div>';

    $form['mc']['cats']['profile_memberlist']['field_membership_category']['#type'] = 'radios';
    $form['mc']['cats']['profile_memberlist']['field_membership_category']['#title'] = t('Membership Category');
    $form['mc']['cats']['profile_memberlist']['field_membership_category']['#description'] = t('Please select your membership category.');
    $form['mc']['cats']['profile_memberlist']['field_membership_category']['#options'] = $membership_cats;


$optionals = array(
    0 => t('No Thanks'),
    1 => t('Optional 1'),
    2 => t('Optional 2'));
$optionals2 = array(
    0 => t('No Thanks'),
    1 => t('Optional 3'),
    2 => t('Optional 4'));


$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#type'] = 'select';
$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#prefix'] = '<div style="background-color:lime;">';
$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#suffix'] = '</div>';
$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#title'] = t('Optional Extras');
$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#options'] = $optionals;

$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#type'] = 'select';
$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#prefix'] = '<div style="background-color:lime;">';
$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#suffix'] = '</div>';
$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#title'] = t('Optional Extras2');
$form['mc']['cats']['profile_memberlist']['field_optional_extras']['#options'] = $optionals2;
share|improve this question

1 Answer

You should do some research before posting such question here. There are lots of tutorials available for ajax like this one.

Then you have option of example module. look into the code of ajax_example & extend your form like that.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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