2

i have form in drupal which uploads images and has got few checkboxes in it. Here is the form:

$form['checklist_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Check List'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,  
  );
$form['checklist_fieldset']['heating'] = array(
   '#type' => 'checkboxes',
   '#title' => t('Heating options'),

   '#options' => array(
  '0' => t('Yes'),
  '1' => t('No')
  ),
   '#description' => t('Heating details.')
  );

and here is my submit function where i am processing image upload and grabbing the checkboxes value as well. I am getting the success message and image is getting uploaded but not getting the value of check boxes.

function property_add_view_submit($form,&$form_state){
$validators = array();



if($file = file_save_upload('p_file1',$validators,file_direcotry_path)){
$heating = array_keys($form_state['values']['heating']);
drupal_set_message(t('Property Saved! '.$heating));
dpm( $form_state['values']['heating']);
}
Maverick
  • 2,738
  • 24
  • 91
  • 157

2 Answers2

5

When you use #options on a FAPI element the value passed to the $form_state is the array key, so you don't need to use array_keys().

I'm not sure why you're using checkboxes for a yes/no, usually one would use a simple checkbox element. However if that's really what you want to do:

  1. Your #options can't contain on option with 0 as the array key, it will be automatically filtered out and you'll never know if that option has been checked.
  2. You should use $heating_options_chosen = array_filter($form_state['values']['heating'] to get the selected checkbox options.

I honestly think your code should look like this though:

$form['checklist_fieldset']['heating'] = array(
 '#type' => 'checkbox',
 '#title' => t('Heating options'),
 '#options' => array(
   '1' => t('Yes'),
   '0' => t('No')
  ),
  '#description' => t('Heating details.')
); 



$heating_checked = $form_state['values']['heating'] == 1;
Clive
  • 36,918
  • 8
  • 87
  • 113
  • well..iam using checkboxes because i wanted to use it as a check box group. – Maverick Mar 31 '12 at 13:25
  • ah ok that makes more sense, glad the answer helped either way – Clive Mar 31 '12 at 14:57
  • 1
    How to toggle a text field depending on the value/options of checkbox? @Clive – Prerit Mohan Apr 18 '13 at 11:50
  • @PreritMohan For Drupal 6 you'll need to do it manually I think, but for Drupal 7 you can use [`#states`](http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#states). – Clive Apr 18 '13 at 11:51
  • @PreritMohan If you need to ask a new question use the link at the top of the page, putting it in comments isn't useful to anyone – Clive Apr 18 '13 at 11:55
  • http://drupal.stackexchange.com/questions/70410/toggle-text-field-depending-on-checkbox-value-in-drupal-6 Can you help now? @Clive – Prerit Mohan Apr 18 '13 at 12:03
  • useful array_filter() to omit non checked box, thanks! – khaled_webdev Sep 29 '16 at 15:49
0

If I have checkbox Friends and options are like

[ ] abc  
[ ] def  
[ ] ghi  
[ ] jkl 

And I want to know which options user have marked, then use below function.

if ($form_state->getValue('friends') != NULL) {

  foreach ($form_state->getValue('friends') as $key => $value) {

    if ($value != 0) {
      $friends = $friends . ", " . $key;
      $friends = substr_replace($friends, "", 0, 1);
    }
  }
}

If user has chosen abc and ghi then you will get 1,3 as result in $friends

If you wanted to know the value then use $friends = $friends.", ".$value;

it worked for me..hope it will help you as well :)

PADMAJA SONWANE
  • 129
  • 1
  • 4