因此,当用户单击“最终会员”表单的更新按钮时,我将同时使用jQuery和pHp向自己发送电子邮件。但是,仅在用户使用Chrome,IE和Microsoft Edge时发送电子邮件。使用Safari和Firefox时,它不起作用。我正在使用点击事件监听器将JSON发送到我的pHp文件。 JSON最初是由一个函数创建的对象,该函数检查两个不同对象之间的差异。这些对象是使用DOM遍历创建的。在该pHp文件中有一个mail()函数,该函数将上述JSON发送给我到我的电子邮件中。我尝试在测试站点上复制该过程,并注意到当我没有添加单击侦听器之前的jQuery时,确实确实从Safari和Firefox发送了电子邮件。但是,如果我添加了jQuery代码,然后将其删除并再次测试,它将不会发送!好像我的服务器被永久拒绝了。这是我的JS代码:

(function($){

    $(document).ready(function(){

    console.log('mailajax is enqueued, showing on firefox');

    var ogArray = new Array(),
        newArray = new Array(),
        dropOgArray = new Array(),
        dropNewArray = new Array(),
        difference,
        username = $('.um-name').find('a').attr('title');


    function diffObject(a, b) {
      return Object.keys(a).reduce(function(map, k) {
        if (a[k] !== b[k]) map[k] = b[k];
        return map;
      }, {});
    }

    $('input.um-form-field').each(function() {

        var $key = $(this).closest('.um-field').find('label').text();
        var $value = $(this).val();

        ogArray[$key] = $value;

    });

    console.log(ogArray);

    setTimeout(function(){

    $('span.select2-chosen').each(function() {

        var $key = $(this).closest('.um-field').find('label').text();
        var $value = $(this).text();

        // console.log($value);

        dropOgArray[$key] = $value;

    });

    console.log(dropOgArray);

    },1000);



    $('input.um-form-field').on('keyup', function(){
        $('form').find('input.um-form-field').each(function() {

            var $key = $(this).closest('.um-field').find('label').text();
            var $value = $(this).val();

            newArray[$key] = $value;

        });

        console.log(newArray);

        console.log(diffObject(ogArray, newArray));

        difference = diffObject(ogArray, newArray);
    });

    $('select.um-form-field').on('change', function(){
        setTimeout(function(){
            $('form').find('span.select2-chosen').each(function() {

                var $key = $(this).closest('.um-field').find('label').text();
                var $value = $(this).text();

                dropNewArray[$key] = $value;

            });

            console.log(diffObject(dropOgArray, dropNewArray));

            dropDifference = diffObject(dropOgArray, dropNewArray);
        }, 1000);


    });


    $('.um-profile-body .um-button').on('click', function(e) {

        $('form').on('submit', function(){

            console.log('form was sent successfully');
            var ajaxurl = 'http://www.reformeducators.org/wp-content/themes/NATE/admin-ajax.php';
                stringDifference = JSON.stringify(difference);
                stringDropDifference = JSON.stringify(dropDifference);
                stringUsername = String(username);


            $.post(ajaxurl, {'Name': stringUsername, 'Changes Made': stringDifference, 'Drop Down Menu Changes': stringDropDifference});


        });


    });


    });

    })(jQuery);


这是我的pHp代码:

<?php

$message = "User Information has been changed\r\n";
$message .= print_r($_POST, true);


$to = "testing@domain.com";
$subject = "User information has been changed!";
$headers = "From: ";

mail($to, $subject, $message, $headers);




?>


我认为这可能是服务器问题,但是如果有人在执行类似操作方面有任何经验,我将非常感谢您的反馈或帮助。

最佳答案

因此,事实证明,在Safari和Firefox上,页面将在发送电子邮件之前刷新。解决方法是,我创建了另一个按钮,用户必须先单击该按钮,然后再单击更新其个人资料信息的实际按钮。第一个按钮上的click事件处理程序现在用于将信息发送到php文件。它解决了问题,无论用户从哪个浏览器更新其个人资料,现在我都收到电子邮件!

这是JavaScript:

(function($){

$(document).ready(function(){

// console.log('mailajax is enqueued, showing on firefox');

setTimeout(function(){

if($('html').hasClass('user-section')){
    // console.log('this is a user page');
    $('input.um-button').hide();
    $('.um-profile .um-col-alt .um-left.um-half').prepend('<a id="custom-update-btn">Approve Changes</a>');
}


var ogArray = new Array(),
    newArray = new Array(),
    dropOgArray = new Array(),
    dropNewArray = new Array(),
    difference,
    username = String($('.um-name').find('a').attr('title'));

function diffObject(a, b) {
  return Object.keys(a).reduce(function(map, k) {
    if (a[k] !== b[k]) map[k] = b[k];
    return map;
  }, {});
}

$('input.um-form-field').each(function() {

    var $key = $(this).closest('.um-field').find('label').text(),
        $value = $(this).val();

    ogArray[$key] = $value;

});

$('span.select2-chosen').each(function() {

    var $key = $(this).closest('.um-field').find('label').text(),
        $value = $(this).text();


    dropOgArray[$key] = $value;

});



$('input.um-form-field').on('keyup', function(){
    $('form').find('input.um-form-field').each(function() {

        var $key = $(this).closest('.um-field').find('label').text(),
            $value = $(this).val();

        newArray[$key] = $value;

    });

});

$('select.um-form-field').on('change', function(){
    setTimeout(function(){
        $('form').find('span.select2-chosen').each(function() {

            var $key = $(this).closest('.um-field').find('label').text(),
                $value = $(this).text();

            dropNewArray[$key] = $value;

        });

        // console.log(diffObject(dropOgArray, dropNewArray));


    }, 1000);


});



$('a#custom-update-btn').on('click', function(e){
    // console.log('update btn has been clicked on');

    var ajaxurl = 'http://www.reformeducators.org/wp-content/themes/NATE/admin-ajax.php',
                  stringDifference = JSON.stringify(diffObject(ogArray, newArray)),
                stringDropDifference = JSON.stringify(diffObject(dropOgArray, dropNewArray));
    $.post(ajaxurl, { 'Name': username, 'Changes Made': stringDifference, 'Drop Menu Changes': stringDropDifference});

    $('a#custom-update-btn').hide();
    $('.um-profile-body .um-button').show();
});

}, 1000);







});

})(jQuery);

关于php - 当用户单击输入按钮但仅在Chrome,IE和Micorosft Edge上有效时,使用自定义Javascript和pHp向我发送电子邮件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37194567/

10-12 12:28
查看更多