当用户点击输入按钮时,使用自定义脚本和PHP向我发送电子邮件,但仅适用于Chrome、IE和Microsoft Edge

时间:2016-05-13 作者:H.Kim

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

$(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 = "[email protected]";
$subject = "User information has been changed!";
$headers = "From: ";

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




?>
我认为这可能是服务器问题,但如果有人有这样做的经验,我将非常感谢您的反馈或帮助。

1 个回复
SO网友:H.Kim

因此,在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(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果