PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] How to sent ajax request after Successful form submission (Contact Form 7) with multiple within tabs Bootstrap 5

 January 28, 2022     ajax, contact-form-7, jquery, wordpress     No comments   

Issue

I have multiple Contact Form 7 Forms within Bootstrap 5 tab panel. I want to send Ajax request to send some data after each and every successful submission. I used wpcf7mailsent to achieve this. This works only for first tab form. Not for others. All forms within tabs working well and receiving the emails from those. Only thing is Ajax request not firing in other tabs. This is the code I am using so far.

var wpcf7Elm = document.querySelector( '.wpcf7' );
    wpcf7Elm.addEventListener( 'wpcf7mailsent', function( event ) {

        var dataSet = {set of serialized data};

        $.ajax({
            type: "POST",
            url: "url",
            data: dataSet,
            dataType:"JSONP",
            success: function(response) {
                console.log( response );
            },
            error: function (data, errorThrown) {
                console.log(errorThrown);
            }
        });

    }, false );`

This works only for first tab only. Whether I gone through other all tabs, this function only works for First Tab only.

Any help highly appreciated. Thanks


Solution

Looking at the code you included it seems you are only selecting one element instead of all ellements change document.querySelector( '.wpcf7' ); to document.querySelectorAll( '.wpcf7' ); to apply the function to all fields

var wpcf7Elms = document.querySelectorAll( '.wpcf7' );
for(i=0;i<wpcf7Elms.length;i++){
var wpcf7Elm = wpcf7Elms[i];
wpcf7Elm.addEventListener( 'wpcf7mailsent', function( event ) {

    var dataSet = {set of serialized data};

    $.ajax({
        type: "POST",
        url: "url",
        data: dataSet,
        dataType:"JSONP",
        success: function(response) {
            console.log( response );
        },
        error: function (data, errorThrown) {
            console.log(errorThrown);
        }
    });

}, false );
}`


Answered By - Jasper B
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing