function mymodule_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == 'comment_form')
{
$form['#submit'][] = 'my_module_comment_form_submit';
}
}
With the above code, the function
my_module_comment_form_submit() will be called after all other submit
handlers have been run. In this function you can do something like this: function my_module_comment_form_submit($form, &$form_state)
{
// Only run our JS if there are no error messages. So first
// we check to see if there are any error messages.
$messages = drupal_get_messages(NULL, FALSE);
if(!count($messages) || !isset($messages['error']) || !count($messages['error']))
{
// There are no error messages, so we send a custom ajax command
$commands[] = array
(
// this is the JavaScript command we will run after the submit
'command' => 'doSomethingOnCommentFormSubmit',
'message' => t('You submitted the form'),
);
return array('#type' => 'ajax', '#commands' => $commands);
}
}
With the above code, if there are no errors in the form
submission, the AJAX command 'doSomethingOnCommentFormSubmit' will be
called. Now we have to register that in a .js file: Drupal.ajax.prototype.commands.doSomethingOnCommentFormSubmit = function(ajax, response)
{
alert(response.message);
};
The above alerts the message that we passed from our submit handler.For more information on using AJAX commands, see: http://www.jaypan.com/tutorial/calling-function-after-ajax-event-drupal-7
No comments:
Post a Comment