2

I am using Drupal 7. I have created a form using the Form API, in which I defined several checkboxes. The form sends its contents in an email and this works fine at the moment as long as I simply output the values of the fields, with the checkboxes outputting the value if checked (e.g. Elderly) and 0 if not. In making up the body of the email I want the zeros to be omitted and only the values of the checked boxes to appear. I have tried to achieve this by using a shorthand ternary operator to include some html if the box is checked and not if not. I can't get this to work. Here's how I've defined the checkboxes in the form itself:

$hometypes = array(
            'Elderly' => t('Elderly'),
            'Disabled' => t('Disabled'),
            'Council tenant' => t('Council tenant'),
            'Home owner' => t('Home owner')
            );

        $form['personaldetails']['survey']=array(
            '#type'=>'checkboxes',
            '#title'=>t('Please indicate your situation by ticking the appropriate boxes'),
            '#description'=>t('You can tick a box by clicking on it once'),
            '#options'=>$hometypes
            );

Here's where I set up the variable $elderly:

$elderly = $form_state['values']['survey']['Elderly'];

And here's the section of the submit function that is not working:

  .($elderly == 'Elderly' ? '<td>Elderly</td> </tr> <tr> <td></td> <td>' : '<td>').

This returns the 'True' value regardless of whether the box is checked or not. If I change it to be:

  .($elderly <> 0 ? '<td>Elderly</td> </tr> <tr> <td></td> <td>' : '<td>').

it returns the 'False' value regardless of whether the box is checked or not.

This may be a bit of a newbie question, but that's because I'm a bit of a newbie! Any help gratefully received.

1 Answer 1

0

Solved it!

I redefined the keys for my checkboxes as values rather than strings. Like so:

$hometypes = array(
        1 => t('Elderly'),
        2 => t('Disabled'),
        3 => t('Council tenant'),
        4 => t('Home owner')
        );

remembering to change this:

$elderly = $form_state['values']['survey'][1];

which means that this:

($elderly == 1 ? '<td>Elderly</td> </tr> <tr> <td></td> <td>' : '<td>')

works perfectly.

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.