Contact Form 7 AJAX Callback

…衆ロ難τιáo~ 提交于 2019-11-27 07:14:28

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 completed successfully, but mail hasn’t been sent because there are fields with invalid input.
  • wpcf7spam — Fires when an Ajax form submission has completed successfully, but mail hasn’t been sent because a possible spam activity has been detected.
  • wpcf7mailsent — Fires when an Ajax form submission has completed successfully, and mail has been sent.
  • wpcf7mailfailed — Fires when an Ajax form submission has completed successfully, but it has failed in sending mail.
  • wpcf7submit — Fires when an Ajax form submission has completed successfully, regardless of other incidents.

Sauce: https://contactform7.com/dom-events/

vicente

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
});
smesh

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?

I had quite a go at this, and I found that when only the Submit event works, it means that there is a js problem / conflict in your theme.

If it's a custom theme you built, make sure jQuery and jQuery migrate are both loaded, in this order, and that the Contact form 7 js is also loaded in the footer.

Make sure you have wp_head, and wp_footer in your php templates.

For DOM events to work, your form must be in Ajax mode. If the page reloads upon submission, forget about DOM events. If you have the form ID showing up in the URL, same thing. My form was initially not in Ajax mode because the Contact Form JS was not loaded, and jQuery Migrate either.

The form must behave exactly like shown on this page for the DOM events to be fired properly. Once you have that, it should be working.

I've tested this with jQuery 3.3.1 and Migrate 3.0.1 and the following event listener worked:

document.addEventListener( 'wpcf7mailsent', function( event ) {
    console.log('mail sent OK');
    // Stuff
}, false ); 

To check if your theme is the culprit, test your form using Wordpress' default theme, if it works, you know the issue is on your end and not so much in the dev's doc!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!