pass arguments to ajax callback function in drupal 7 form api

user764851 picture user764851 · May 22, 2011 · Viewed 28.5k times · Source

how to pass arguments to Ajax callback function in drupal 7 form api

$element['field name'] = array(
        '#type' => 'textfield',
        '#ajax' => array(
          'callback' => 'ajax_function_name_callback'/%/%/%,
          'method' => 'replace',
          'event' => 'blur',
              'effect' => 'fade',
              'progress' => array('type' => 'throbber', 'message' => ''),
        ),
    );

function ajax_function_name_callback($form,$form_state)
{
return ..
}

for example if i need to specify form element to make action using ajax i need to pass the element name to the function and make customer operation and return the result to another element form

i need passed aruguments to this callback function 'callback' => 'ajax_function_name_callback'

function ajax_function_name_callback($args1,$args2,...$form,$form_state) { return .. }

2 - and how Through the form ?

thanks..

if i dont know what the $input_name it's genrated from something oprations i need to tell ajax_'function_name_callback the name of this field to make

$element[$input_name] = array(
        '#type' => 'textfield',
        '#size' => '41',
        '#ajax' => array(
//////////////////////////////////////////////////////////////////////////////////
// here how i tell the ajax call back about this arguments informationvlike parents of this field ... etc
/////////////////////////////////////////////////////////////////////////////////
              'callback' => 'ajax_'function_name_callback',
          'method' => 'replace',
          'event' => 'blur',
              'effect' => 'fade',
              'progress' => array('type' => 'throbber', 'message' => ''),
        ),
    );


function ajax_'function_name_callback($arg_position,$arg_fieldName,$form,$form_state)
{
$form[$arg_position][$arg_fieldName][#value] = anotherFunction($form[$arg_position][$arg_fieldName][#value]);
return $form[$arg_position][$arg_fieldName];
}

Answer

caboose picture caboose · Jun 17, 2011

Use this $form_state['triggering_element'] this will tell you the name of the triggering element and give you all of its attributes.