-1

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;
-1

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.

  • If you want, contact me via eMail (see my profile) for more details as you asked in your last comment earlier today. BTW (constructive feedback): I bet the downvote of this answer is because it looks pretty close t a "link only" answer". If you want to improve it, you should ADD a relevant quote in your answer of what's include in those inks. So that if the link content ever changes (or becomes broken), your answer still makes sense. – Pierre.Vriens Feb 11 '17 at 14:49

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.