1

I'm attempting to create a a form using the Form API and I'm unable to get checkboxes and radio buttons working properly. Although they show up in the form, only the first option of each one is displayed, that option is displayed with the title of the form:

enter image description here

I also have a select box on the page which is displaying fine, and I'm passing through the values to the select the same way I'm passing it through to the radio/checkboxes, with an array.

How do you pass values through to the #options parameter for checkboxes and radios, or do I simply having a careless error somewhere in my code (featured below)?

Code for the radio buttons and checkboxes:

 $facts = array(
        'Ponycon' => t('Attended Ponycon'),
        'PurhcaseMerch' => t('Purhcased My Little Pony: Friendship is Magic merchandise (dolls, cards, shirts, et cetera)'),
        'PurchaseShow' => t('Purhcased Episodes'),
        'Meetups' => t('Attended Brony meet-ups'),
        'Fanclubs' => t('Part of a MY Little Pony: Friendship is Magic fanclub (Facebook, local, et cetera)'),
    );


$form['fan_activities'] = array(
    '#type' => 'checkbox',
    '#title' => t('Which of the following have you done?'),
    '#options' => $facts,
    '#description' => t('Brony activities'),
);

$fdisc = array(
    'friend' => t('A friend told me about it'),
    'ads' => t('Advertisments (commercials, billboards, et certera)'),
    'Internet' => t('The internet, but not through an ad (forum, website article, et cetera)'),
    'Other' => t('Other'),
);

$form['discoverry'] = array(
    '#type' => 'radio',
    '#title' => t('How did you first hear about My Little Pony: Friendship is Magic?'),
    '#options' => $fdisc,
    '#description' => t('How you discovered the show'),
);

Code for the working select box:

        $fchoices = array(
        'RainbowDash' => t('Rainbow Dash'),
        'TwilightSparkle' => t('Twilight Sparkle'),
        'Rarity' => t('Rarity'),
        'PinkiePie' => t('Pinkie Pie'),
        'Fluttershy' => t('Fluttershy'),
        'Applejack' => t('Applejack'),
    );

    $form['favpony_area']['favpony'] = array(
        '#type' => 'select',
        '#title' => t('Who is your favourite Pony from the mane six?'),
        '#options' => $fchoices,
        '#default_value' => variable_get('favpony', 'RainbowDash'),
        '#description' => t('Your favourite Pony frmo the mane six'),
    );

1 Answer 1

2

The only thing I could suggest would be to try using '#type' => 'radios' and '#type' => 'checkboxes' instead of radio and checkbox. I believe radio and checkbox without the s on the end only show one option as more of an on off option.

Try having a look at Forms API for more explanation

1
  • +1 Thanks, that was it. Never would have thought about that, just assumed that they both would do the same thing.
    – Wipqozn
    Commented Apr 12, 2012 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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