I have a text field, filling which should update several fields with the database values:

<?php
$form
['evanr'] = array(
       
'#type' => 'textfield',
       
'#required' => TRUE,
        ...
       
'#ajax' => array(
         
'callback' => 'ajax_check_evaopgave',
         
'wrapper' => 'replace_sfp',
        ),
);
?>

at the same time I have the field that is currently bonded to be updated:

<?php
$form
['sfp'] = array(
   
'#type' => 'select',
   
'#options' => drupal_map_assoc(
      ...
    ),
   
'#prefix' => '<div id="replace_sfp">',
   
'#suffix' => '</div>',
   
'#value' => (get_sfp_value($form_state['values']['evanr'])),
);
?>

and the supplier methods:

<?php
function ajax_check_evaopgave($form, $form_state) {
  return
$form['sfp'];
}

function
get_sfp_value($evanr){
  ...
//return value from database
}
?>

The problem is that I need to update more than one field, when evanr field is filled.
How to attach multiple ajax callback to the same field?

Thanks in advance

Comments

adrianborrego’s picture

You don't need attach multiple ajax callback to the same field, in only one callback you can update many fields but inside the same wrapper.
See http://drupal.org/project/examples

turtletrail’s picture

thanks, I used this idea and it helped.

However, the examples you gave are not really explaining this concept,
but I also found this description, which is more explanatory in my opinion:

You can easily replace the entire form if that is easiest. Just add a #prefix and #suffix to the entire form array, then set that as the #ajax['wrapper']. (This will allow you to change multiple form elements via a single ajax call.) The only reason not to do this is that the process is faster if less information is transferred.

from AJAX Forms in Drupal 7, which basically extends your idea.

adrianborrego’s picture

Thanks to you too.

turtletrail’s picture

I am having troubles with a new thing, but the root is the same - drupal's ajax functionality.

I discovered, that the problem comes from ajax '#path' attribute.

if it is set, ajax functionality is not doing its job - updating fields:

<?php
    $form
['evanr'] = array(
       ...
       
'#ajax' => array(
         
'callback' => 'ajax_check_evaopgave',
         
'wrapper' => 'replace_form',
         
'path' => 'dognrapport/table/new',
         
'method' => 'replace',
        ),
    );
?>

If it is not set, ajax functionality is working just fine:

<?php
    $form
['evanr'] = array(
             ...
       
'#ajax' => array(
         
'callback' => 'ajax_check_evaopgave',
         
'wrapper' => 'replace_form',
         
'method' => 'replace',
        ),
    );
?>

but on form submit, i'm offered to save json file, and the form is not submitted.

Any help appreciated

turtletrail’s picture

the solution was simple it is just adding $form['#action'] with right url.

seems like ajax is changing the page url, which is hidden.
That is why submit didn't work unless i explicitly specified the action url.

lwlewy’s picture

It's possible to update two different element, even if they are on different part of the site. On this way it worked at me:

function callback_function(&$form, &$form_state)
{
  $select1 = array();
  $select2 = array();
 
  (...)

  $commands = array();
  $commands[] = ajax_command_html('#element1', drupal_render($select1));
  $commands[] = ajax_command_replace("#element2", drupal_render($select2));
  return array('#type' => 'ajax', '#commands' => $commands);
}

amar.deokar’s picture

This is worked for me

function callback_function($form, &$form_state)
{
  $commands = array();
  $commands[] = ajax_command_replace('#element1', drupal_render($form[any form element]));
  $commands[] = ajax_command_replace("#element2", drupal_render($form[any form element]));
  return array('#type' => 'ajax', '#commands' => $commands);
}

amar deokar