4

For example:

/** FIRST FIELDSET **/

$form['fieldset_1']['enabled'] = array(
  '#type'        => 'radios',
  '#options'     => array(1 => t('Enable'),
                          0 => t('Desable'),
                          ),
  '#ajax'    => array('callback' => 'my_ajax_callback',
                      'wrapper'  => 'my_wrapper_1',
                     ),
);

$form['fieldset_1']['myChangebleField'] = array(
  '#type'   => 'whatever',
  '#prefix' => '<div id="my_wrapper_1">',
  '#suffix' => '</div>',
);

/** SECOND FIELDSET **/

$form['fieldset_2']['enabled'] = array(
  '#type'        => 'radios',
  '#options'     => array(1 => t('Enable'),
                          0 => t('Desable'),
                          ),
  // Now I am calling the same callback function but assigning a different wrapper
  '#ajax'    => array('callback' => 'my_ajax_callback',
                      'wrapper'  => 'my_wrapper_2',
                     ),
);

$form['fieldset_2']['myChangebleField'] = array(
  '#type'   => 'whatever',
  '#prefix' => '<div id="my_wrapper_2">',
  '#suffix' => '</div>',
);

function my_ajax_callback($form, $form_state) {

/**  
 *  How do I figure out which of the element "enabled" was the caller? 
 *
 *  In that way I could do something like:
 **/
   switch($caller){
     case 'fieldset_1':
       // do stuff...
       break;
     case 'fieldset_2':
       // do stuff...
       break;
     // ...
   }

   return $form[$fieldset]['myChangebleField']
}

As seen in the example above, how do I figure out which of the element "enabled" was the caller?

Having two callback functions is not an option as the form elements will be constructed in a dynamic way and I won't actually use a switch statement in the callback function as shown in the example.

Cheers,

0

1 Answer 1

8

Got it!!! This guy here $form_state['triggering_element'] does the trick.

Your Answer

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

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