在addeventlistener wordpress中POST联系表单7

时间:2018-03-01 02:55:42

标签: wordpress contact-form-7

我是javascript的新手,我正在尝试使用addeventlisterner将我的联系表单7数据发布到我的另一个页面,它正在我的控制台中工作但是一旦我添加了我的ajax它就不会...如果有什么问题,检查我的代码?感谢

这是我的代码与工作控制台

    document.addEventListener( 'wpcf7mailsent', function( event ) {

    var inputs = event.detail.inputs;

    if ( '1286' == event.detail.contactFormId ) {

        var fullname = document.getElementById('very-special-field').value; 
        var msisdn = document.getElementById('mobile-number').value; 
        var username = document.getElementById('username').value;
        var email = document.getElementById('email').value;
        var company = document.getElementById('company').value;
        var account_type_id = document.getElementById('account_type_id').value;

        form_data = {
            fullname,
            msisdn,
            username,
            email,
            company,
            account_type_id

        }

        alert('hi');

        console.log(form_data);

    }

`}, false );

但是,如果我添加我的ajax,它就无法正常工作

document.addEventListener( 'wpcf7mailsent', function( event ) {

    var inputs = event.detail.inputs;

    if ( '1286' == event.detail.contactFormId ) {

        var fullname = document.getElementById('very-special-field').value; 
        var msisdn = document.getElementById('mobile-number').value; 
        var username = document.getElementById('username').value;
        var email = document.getElementById('email').value;
        var company = document.getElementById('company').value;
        var account_type_id = document.getElementById('account_type_id').value;

        form_data = {
            fullname,
            msisdn,
            username,
            email,
            company,
            account_type_id

        }
        $.ajax({
          type: "POST",
          url: http://myurl.com,
          data: JSON.stringify(form_data),
          success: success,
         dataType: 'json'
        });

        alert('hi');

        console.log(form_data);

    }

}, false );

有人能帮助我吗? :(我这样做对吗?

1 个答案:

答案 0 :(得分:0)

试试这个脚本

    document.addEventListener( 'wpcf7mailsent', function( event ) {

        var inputs = event.detail.inputs;

        if ( '1286' == event.detail.contactFormId ) {

            var fullname = document.getElementById('very-special-field').value; 
            var msisdn = document.getElementById('mobile-number').value; 
            var username = document.getElementById('username').value;
            var email = document.getElementById('email').value;
            var company = document.getElementById('company').value;
            var account_type_id = document.getElementById('account_type_id').value;

            form_data = {
                fullname,
                msisdn,
                username,
                email,
                company,
                account_type_id

            };

            jQuery.ajax({
              type: "POST",
              url: 'http://myurl.com',
              data: JSON.stringify(form_data),
              success: function(data) {
                console.log(data);
              },
              error: function(data) {
                console.log(data);
              },
             dataType: 'json'
            });
            alert('hi');
            console.log(form_data);
        }

    }, false );