pass arguments to ajax callback function in drupal 7 form api
how to pass arguments to Ajax callback function in drupal 7 form api
$element['field name'] = array(
'#type' => 'textfield',
'#ajax' => array(
'callback' => 'ajax_function_nam开发者_如何学Goe_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];
}
Use this $form_state['triggering_element']
this will tell you the name of the triggering element and give you all of its attributes.
Through the form. No need for a custom callback.
The ajax callback has access to all information that is part of the form. You can for example add a form element with type hidden (sent to the browser, can be changed with JavaScript for example) or value (only used internally, can contain any kind of data like objects and can not be changed by the user).
If you can give a more detailed example of what you want to do, I can give you a more detailed explanation.
There is no way to pass additional arguments in ajax callback function.
See includes/form.inc on line 381 for more details.
function ajax_form_callback() {
list($form, $form_state) = ajax_get_form();
drupal_process_form($form['#form_id'], $form, $form_state);
// We need to return the part of the form (or some other content) that needs
// to be re-rendered so the browser can update the page with changed content.
// Since this is the generic menu callback used by many Ajax elements, it is
// up to the #ajax['callback'] function of the element (may or may not be a
// button) that triggered the Ajax request to determine what needs to be
// rendered.
if (!empty($form_state['triggering_element'])) {
$callback = $form_state['triggering_element']['#ajax']['callback'];
}
if (!empty($callback) && function_exists($callback)) {
return $callback($form, $form_state); // TA-DAM!
}
}
The #ajax has an element called parameters, and can use as follows,
$form['pro_div6_'.$id] = array(
'#type' => 'button',
'#value' => t('order now'),
'#ajax' => array(
'wrapper' => 'order_wrapper',
'callback' => 'product_order_callback',
'parameters'=>array('param1'=>'param1_vale','param2'=>'param2_vale','param3'=>'param3_vale'),
),
);
And you can get this values from the call back function as,
function product_order_callback($form, $form_state){
echo "<pre>";print_r($form_state['triggering_element']); exit;
}
will get an output like,
![Please find the screen shot][1]
[1]: http://i.stack.imgur.com/75PoU.jpg
Enjoy the coding :)
i also have the same problem. My solution for that is to define a hidden field for the form and then get the hidden value in the callback. For example:
$form['departure_city_1']= array(
'#type' => 'select',
'#options'=>$destination_array,
'#weight'=>1,
'#ajax' => array(
'callback' => 'ajax_example_autocheckboxes_callback',
'wrapper' => 'checkboxes-div',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['skybird_token']= array(
'#type' => 'hidden',
'#value' => $token,
);
And then in the callback:
function ajax_example_autocheckboxes_callback($form, $form_state) {
$token=$form_state['values']['skybird_token'];
}
Some conclusion of a possible ways to passing variables into the AJAX-callback:
- Save your variable to the $form_state['YOUR_VAR_NAME'], and then use in callback.
- Create a form element with a hidden type like proposed before.
Save your variable into a some of form element attributes like:
$element['field_name'] = array( '#type' => 'textfield', '#ajax' => array( 'callback' => 'ajax_function_name_callback', 'method' => 'replace', 'event' => 'blur', 'effect' => 'fade', 'progress' => array( 'type' => 'throbber', 'message' => '', ), ), '#attributes' => array( 'data' => array('some_data'), 'id' => array('some_id'), ), ); function ajax_function_name_callback($form,$form_state) { $data = $form['field_name']['#attributes']['data'][0]; $id = $form['field_name']['#attributes']['id'][0]; }
you can use $form_state and pass it to callback function, for example
define in form function like this
function form_function_name($form, $form_state){
$element['field name'] = array(
'#type' => 'textfield',
'#ajax' => array(
'callback' => 'ajax_function_name_callback',
'method' => 'replace',
),
);
$form_state['var'] = array('variable' => 'My_costome_variable');
return $element;
}
function ajax_function_name_callback($form, $form_state){
# use of $form_state['var'];
return ..
}
pay attention if you want to change form_state['var'] in ajax_function_name_callback you must use
function ajax_function_name_callback($form, &$form_state)
instead of
function ajax_function_name_callback($form, $form_state)
精彩评论