Hi,
I am trying to add an ajax callback to my simplenews subscription form. The problem is that the callback is only called when the message alert returns an error message (with classes: 'messages error'). The callback isn't called when the form returns a positive message (with classes: 'messages status').
I followed the given instructions and added the following code to a custom module. I also tried to add the callback in the original form that defines it in simplenews.module. It had the same result.
<?php
function custom_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'simplenews_block_form_10') {
$form['#ajaxsubmit'] = TRUE;
$form['#ajaxsubmit_callbacks'] = array('subscriptionCallback',);
}
}
?>
I placed the callback function in a javascript file called by the theme:
$(document).ready(function(){
/**
* Newsletter ajaxsubmit submission handler.
**/
subscriptionCallback = function (target, data) {
alert('test');
var messages = $('div.messages', target);
if (messages && !messages.hasClass('error')) {
alert('test: no error class');
setTimeout(function () {
// remove the values from the form inputs
$('#simplenews-block-form-10 input.form-text').each(function (i, el) {
el.value = '';
});
messages.slideUp();
}, 3000);
}
}
}); // end .ready function
Thank you very much in advance. I appreciate it very much.
Cheers,
Danny
Comments
Comment #1
danny_joris commentedActually I tried this on a webform on the same website and the results were the same. Only a callback returned when the form submit resulted in giving an error message...
Comment #2
katbailey commentedAh, just figured it out - you need $form['#redirect'] = FALSE; for your callback to work, so your hook_form_alter would be like this:
Comment #3
danny_joris commentedThank you katbailey, this works! So the form made a redirect before?
I changed the code a little you gave me to give it a fadeout instead of a slide up. I am wondering: what would i need to do add a fade in to the messages as they appear? Is it something in the callback. I'm not a pro, but I don't see any calls for the message in the callback that I could edit, but it's very likely that i'm wrong.
Again, thanks a million for your help !! :)
Danny
Comment #4
katbailey commentedMake the messages div invisible by default just using css, i.e. in your css file:
Then in your callback function
You'd want to make sure that this works fine without js though too. The form will just submit normall but if the .messages class is still used in the non-js version then they'll never get shown.
Comment #5
danny_joris commentedThat works! thank you very very much, katbailey !! :)