Hi there,
sorry but i have 3 questions:

1- How can i trigger ajax on a specifix choice of radio button in a radios list?
Adding the ajax attribute to the form element triggers the ajax function on every value change!

<?php
$form
['act_type'] = array(
       
'#type' => 'radios',
       
'#title' => t('Type'),
       
'#options' => drupal_map_assoc(array(
           
t('Art'),
           
t('Conference'),
           
t('Music'),
           
t('Other'),
        )),
       
'#default_value' => empty($form_state['values']['act_type']) ? '' : $form_state['values']['act_type'],
       
'#required' => TRUE,
       
'#ajax' => array(
           
'wrapper' => 'act-type-other-div',
           
'callback' => 'show_act_type_other_callback',
           
'effect' => 'fade',
        )
      );
?>

I just want ajax to be triggered when the "Other" radio is chosen.

2- The same question concerning checkboxes.

<?php
$form
['act_type'] = array(
       
'#type' => 'checkboxes',
       
'#title' => t('Type'),
       
'#options' => drupal_map_assoc(array(
           
t('Art'),
           
t('Conference'),
           
t('Music'),
           
t('Other'),
        )),
       
'#default_value' => empty($form_state['values']['act_type']) ? '' : $form_state['values']['act_type'],
       
'#required' => TRUE,
       
'#ajax' => array(
           
'wrapper' => 'act-type-other-div',
           
'callback' => 'show_act_type_other_callback',
           
'effect' => 'fade',
        )
      );
?>

I just want ajax to be triggered when the "Other" checkbox is chosen, although multiple choices can be chosen here.

3- How to get the value of a selected checkbox if it is in a checboxes list?

Thanks in advance for you time and help.

Eagerly waiting reply.

Comments

Using #ajax['trigger_as'] may do the trick but I suspect you are trying to show/hide a textfield bases on that selection in which case you might want to use #states instead, see http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_pr..., the example module also includes an example for this

You're right! I want to show/hide a textfield when the "Other" radio/checkbox is chosen ...
But since i'm these radios and checkboxes lists are in a fieldset ... "#states" doesn't seem to work!
The textfield stays visible at all time in this fieldset.

I've searched for #ajax['trigger_as'] before posting my questions but found no practical examples ... i didn't know how to implement it or get it to work :(

the original code is as follows:

<?php
$form
[step2]['act_type'] = array(
       
'#type' => 'radios',
       
'#title' => t('Type'),
       
'#options' => drupal_map_assoc(array(
           
t('Art'),
           
t('Conference'),
           
t('Music'),
           
t('Other'),
        )),
       
'#default_value' => empty($form_state['values'][step2]['act_type']) ? '' : $form_state['values'][step2]['act_type'],
       
'#required' => TRUE,
       
'#ajax' => array(
           
'wrapper' => 'act-type-other-div',
           
'callback' => 'show_act_type_other_callback',
           
'effect' => 'fade',
        )
      );
?>

As you can see "step2" is the field set that prevents #states from working right

I guess i figured out the problem that prevents the textfield from hiding and keeps it visible at all time ... but doesn't have a clue how to fix it :S

This is the code from the examples:

<?php
$form
['student_type'] = array(
   
'#type' => 'radios',
   
'#options' => drupal_map_assoc(array(t('High School'), t('Undergraduate'), t('Graduate'))),
   
'#title' => t('What type of student are you?')
  );
 
$form['high_school'] = array(
   
'#type' => 'fieldset',
   
'#title' => t('High School Information'),
   
// This #states rule says that the "high school" fieldset should only
    // be shown if the "student_type" form element is set to "High School".
   
'#states' => array(
     
'visible' => array(
       
':input[name="student_type"]' => array('value' => t('High School')),
      ),
    ),
  );
?>

working like a charm ...

but adding a container element (div or fieldset) before the "student_type" messes everything up

<?php
$form
['step2'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Step 2:'),
      );
$form['step2']['student_type'] = array(
   
'#type' => 'radios',
   
'#options' => drupal_map_assoc(array(t('High School'), t('Undergraduate'), t('Graduate'))),
   
'#title' => t('What type of student are you?')
  );
 
$form['step2']['high_school'] = array(
   
'#type' => 'fieldset',
   
'#title' => t('High School Information'),
   
// This #states rule says that the "high school" fieldset should only
    // be shown if the "student_type" form element is set to "High School".
   
'#states' => array(
     
'visible' => array(
       
':input[name="student_type"]' => array('value' => t('High School')),
      ),
    ),
  );
?>

do you know how to fix that?
and how to use #ajax['trigger_as']? an example of this would be appreciated :)

I use firebug for this, I inspect the radios for student_type and see what name is used the use that in the code.

You originally asked about checkboxes, the examples in form_examples_states.inc includes an example for that also.

Hey nevets, thanks for your time .... and your help ... I FINALLY GOT IT
you gave me a push to dig deeper into states ...

To fix the above code just add the fieldset name, in which the elements are contained, before the element name to check, thus the code becomes as follows:

<?php
$form
['step2'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Step 2:'),
      );
$form['step2']['student_type'] = array(
   
'#type' => 'radios',
   
'#options' => drupal_map_assoc(array(t('High School'), t('Undergraduate'), t('Graduate'))),
   
'#title' => t('What type of student are you?')
  );
 
$form['step2']['high_school'] = array(
   
'#type' => 'fieldset',
   
'#title' => t('High School Information'),
   
// This #states rule says that the "high school" fieldset should only
    // be shown if the "student_type" form element is set to "High School".
   
'#states' => array(
     
'visible' => array(
       
':input[name="step2[student_type]"]' => array('value' => t('High School')),
      ),
    ),
  );
?>

As u can see, just adding the fieldset "step2" name before the name of the element to be checked, in the input selector name, fixes the problem ...

THANK YOU .... and hope this helps u as well

Facing another problem now!!!

The previous solution worked perfectly with a radio, radios list, and a single checkbox.

But i need to apply it on a multiple choice checkboxes list!
The problem is that the checkboxes list can have multiple values, so how can i check that a specific chechbox is checked.
I'll try to explain more, see, I have 3 choices here.
I want: Selecting the first 2 choices doesn't trigger anything, but selecting the "Other" choice shows the hidden textfield:

<?php
$form
['step2'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Step 2:'),
      );
$form['step2']['student_type'] = array(
   
'#type' => 'checkboxes',
   
'#options' => drupal_map_assoc(array(t('High School'), t('Undergraduate'), t('Other'))),
   
'#title' => t('What type of student are you?')
  );
 
$form['step2']['high_school'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Other'),
   
'#states' => array(
     
'visible' => array(
       
':input[name="step2[student_type]"]' => array('value' => t('Other')),
      ),
    ),
  );
?>

How can I check that the 3rd checkbox (in this case "Other") is selected?
If we use 'value', as in the above example, all the checked values are taken into consideration, but i only want to check if "Other" is checked to show the hidden element.

Thanks in advance ...

Eagerly waiting your reply.

HAHAHAHAHA ....

I got it again ... debugged it using my personal favorite, the awesome addon ,"FireBug"
Simply just add the generated checkbox name of "Other" to the input-selector name, and check the 'checked' attribute instead of the 'value' attribute.
The code becomes as follows:

<?php
$form
['step2'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Step 2:'),
      );
$form['step2']['student_type'] = array(
   
'#type' => 'checkboxes',
   
'#options' => drupal_map_assoc(array(t('High School'), t('Undergraduate'), t('Other'))),
   
'#title' => t('What type of student are you?')
  );
 
$form['step2']['high_school'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Other'),
   
'#states' => array(
     
'visible' => array(
       
':input[name="step2[student_type][Other]"]' => array('checked' => TRUE),
      ),
    ),
  );
?>

Hope this also helps ...

Happy Programming :)

For information now there is change,
here is an update of your code

      'visible' => array(
        ':input[name="student_type"]' => array('value' => t('High School')),
      ),

the "step2" has to be remove to make it work

<?php
$form
['step2'] = array(
       
'#type' => 'fieldset',
       
'#title' => t('Step 2:'),
      );
$form['step2']['student_type'] = array(
   
'#type' => 'radios',
   
'#options' => drupal_map_assoc(array(t('High School'), t('Undergraduate'), t('Graduate'))),
   
'#title' => t('What type of student are you?')
  );
 
$form['step2']['high_school'] = array(
   
'#type' => 'fieldset',
   
'#title' => t('High School Information'),
   
// This #states rule says that the "high school" fieldset should only
    // be shown if the "student_type" form element is set to "High School".
   
'#states' => array(
     
'visible' => array(
       
':input[name="student_type"]' => array('value' => t('High School')),
      ),
    ),
  );
?>

I guess it should be the same with checkboxe.

Anyway thanks, now I can remove my useless jQuery for this one :D