2

How do I call a javascript function after a successful ajax form submit? I want to call a function in a file that I added with drupal_add_js() in my_form_name_form($form, &$form_state).

0

3 Answers 3

3

Have a look at the Ajax framework and Ajax framework commands topics in the API documentation.

Basically, instead of simply returning a single render array, you can return a number of so called ajax commands, which will be translated to javascript and executed on the client side. Replacing the specified wrapper with the returned content is just the default command that is done when nothing else is specified.

There doesn't seem to be a provided command that allows to execute any javascript method (only an invoke command that allows to execute an jQuery command on a defined selector) though. However, you can either define your own command (just have a look at what these helper functions return and the corresponding JS functions in misc/ajax.js (look for Drupal.ajax.prototype.commands = {...).

Or you might be able to replace your custom JS function with a number of the provided default ajax commands. They're pretty flexible and I'm for example using them to update the page title in userpoints.module.

1

Maybe this can work for you as well. It worked for me on Ajax Views Exposed Filters.

jQuery(document).ajaxComplete(function(e, xhr, settings) {
  // Call your function here  
}); 
0

The case that worked for me. I have created MYMODULE.js file and included it in MYMODULE.info

scripts[] = PATH/TO/MYMODULE.js

In MYMODULE.js I inserted following code

(function ($) { $(document).ready(function(){ 
$("#ID_OF_SUBMIT_BUTTON").click(function(event){

// DO SOMETHING

}); 
})(jQuery);

Also made following settings for form

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Continue'),



'#id' => drupal_html_id('ID_OF_SUBMIT_BUTTON'),
'#ajax' => array(
  'callback' => 'ajax_form_submit_callback',
  'wrapper' => '',
  'event' => 'click',
),

);

#id was used to make sure that jquery function will be associated with this form only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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