0

I need to execute some functions when a field value changes. I tried both Drupal AJAX framework and classical Jquery, but in both cases the callback function is not executed. Adding a Jquery .change() :

(function ($, Drupal, window, document, undefined) {
  $(document).ready(function(){
    $("#foo").change( function(){
      var selected_date = $(this).val();
      $.ajax({
        type: "POST",
        url: "MYURL/"+selected_date ,
        data: selected_date,
        success: alert( selected_date)
      });
    });
  })
})(jQuery, Drupal, this, this.document);

The Jquery works fine. Then

/**
* Implementation of hook_menu().
*/
function MYMODULE_menu() {
  $items['MYURL/%'] = array(
    'page callback' => 'MYMODULE_get_liberte_id', 
    'page arguments' => array(1),
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
  );
  return $items;
}

function MYMODULE_get_liberte_id ($selected_date) {
  //DB queries and form alter or error messages here
}

I also tried with AJAX Framework :

//hook form alter
$form['foo']['#ajax'] = array(
  'callback' => 'MYMODULE_ajax_callback',
  'wrapper' => '.center-wrapper',
 );

function MYMODULE_ajax_callback($form, $form_state) {
  //is the function fired ???
  $commands[] = ajax_command_alert(t('MYMODULE_ajax_callback fired'));
  return '<div id="wrapper">'.date('Y-m-d').'<xmp>'.print_r($form_state['values'], true).'</xmp></div>';
  //DB queries and form alter or error messages here
}

dsm($form) shows that the field got #ajax attribute, but the callback function isn't executed.

I'm afraid I'm not on the right way, or at list I'm missing something :(

1 Answer 1

1

The #ajax['wrapper'] value should be id attribute, not a class, and without '#'. You can look about this here. The callback function should return a html content, a renderable array (which will replace the 'wrapper' on a call) or array of drupal ajax commands. You can try this and your code should work:

function MYMODULE_ajax_callback($form, $form_state) {
   $commands[] = ajax_command_alert(t('MYMODULE_ajax_callback fired'));
   return array('#type' => 'ajax', '#commands' => $commands);
}
3
  • Thanks :) Indeed I forgot to return sthing !!! But return $commands didn't work, I had to use return array('#type' => 'ajax', '#commands' => $commands); Anyway I'm still fighting a step forward... How can I return updates for $form AND $form_state at the same time, and for various fields of the form ?
    – Kojo
    Commented May 28, 2014 at 17:50
  • Ah yes, I updated the answer. You don't need return $form_state or $form. In your hook_form_alter $form_state should be already updated with it's values (for this I think you need to take $form_state by reference in ajax callback function). So you can change the form depending on form state values and also add to $commands array something like this: $commands[] = ajax_command_replace('#selector', drupal_render($form['something']));
    – Saulius
    Commented May 29, 2014 at 7:27
  • well, I'm afraid I don't understand the exact possibilities of the callback fn... I tried to change form_state['values'] by reference but nope. I think I'll have to post another question... As you inserted my comment in the answer I'll mark it as accepted. Thanks a lot
    – Kojo
    Commented May 29, 2014 at 18:56

Your Answer

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

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