Contact Form 7 AJAX Callback
In version 3.3 new jQuery custom event triggers were introduced:
New: Introduce 5 new jQuery custom event triggers
- wpcf7:invalid
- wpcf7:spam
- wpcf7:mailsent
- wpcf7:mailfailed
- wpcf7:submit
You can use wpcf7:invalid
like the example below:
$(".wpcf7").on('wpcf7:invalid', function(event){ // Your code here});
Given the variety of responses on this topic the plugin developer seems to change their mind about how this should work every 5 minutes. Currently (Q1 2017) this is the working method:
document.addEventListener( 'wpcf7mailsent', function( event ) { alert( "Fire!" );}, false );
And the valid events are:
- wpcf7invalid — Fires when an Ajax form submission has completedsuccessfully, but mail hasn’t been sent because there are fields withinvalid input.
- wpcf7spam — Fires when an Ajax form submission hascompleted successfully, but mail hasn’t been sent because a possiblespam activity has been detected.
- wpcf7mailsent — Fires when an Ajaxform submission has completed successfully, and mail has been sent.
- wpcf7mailfailed — Fires when an Ajax form submission has completedsuccessfully, but it has failed in sending mail.
- wpcf7submit — Fireswhen an Ajax form submission has completed successfully, regardlessof other incidents.
Sometimes it may not work, as Martin Klasson pointed out, only 'submit' event works, most probable because it's triggered by a form and bubbles up to the selected object. Also as I can understand, now events have other names, like "invalid.wpcf7", "mailsent.wpcf7", etc. In short, this should work:
jQuery('.wpcf7').on('invalid.wpcf7', function(e) { // your code here});
More detailed explanation here: How to add additional settings on error in Contact form 7?