0

Hi I am using #ajax in my custom form. I need a response handler function for my form in jquery. How can i acheive this goal?

For example:

$form['changethis'] = array(
'#type' => 'select',
'#options' => array(
  'one' => 'one',
  'two' => 'two',
  'three' => 'three',
),
'#ajax' => array(
  'callback' => 'ajax_example_simplest_callback',
  'wrapper' => 'replace_textfield_div',
 ),
);

Here i have the ajax_example_simplest_callback in php. I need a response handler in js.

3 Answers 3

1

Try this

(function($) {

  Drupal.YOUR_MODULE_NAME = {
    form_id: 'FORM_ID'  
  };

  Drupal.behaviors.YOUR_MODULE_NAME = {
   attach: function(context, settings) {

    for (ajax_el in settings.ajax) {
      if (Drupal.ajax[ajax_el].element.form) {
        if (Drupal.ajax[ajax_el].element.form.id === Drupal.YOUR_MODULE_NAME.form_id) {
          Drupal.ajax[ajax_el].success = Drupal.YOUR_MODULE_NAME.success;
       }
      }
     }
    }   
   };


 Drupal.YOUR_MODULE_NAME.success = function (response, status) {
    // HERE YOU GET THE response
 }

 });
1
0

You should look into examples modules. ajax_example module has a number of examples.

eg. dependent dropdown

2
  • I check this examples. Please consider the question before answering. 'Here i have the ajax_example_simplest_callback in php. I need a response handler in js.'
    – Sachin T
    Commented Mar 19, 2013 at 10:45
  • I am not sure what exactly you are trying to achieve, you can add js response by $commands = array(); $commands[] = ajax_command_*() return array('#type' => 'ajax', '#commands' => $commands);
    – Deepak ML
    Commented Mar 19, 2013 at 10:54
0

This is not the cleanest way of doing things but does the job none the less...

'#ajax' => array(
  'progress' => array(
    'type' => 'throbber',
    'message' => '<script>YOUR_CUSTOM_HANDLER(); </script>',
  ),

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.