jQuery Ajax不将数组作为数据对象发送

时间:2015-09-03 18:13:22

标签: javascript jquery arrays ajax post

我正在构建一个联系表单,当用户点击提交按钮时,它会从ajax调用向公司发送一封电子邮件。但是,它没有将数组变量从表单传递给ajax PHP文件。在成功将数组变量记录到控制台时似乎有效。但电子邮件中缺少数据。以下是我的代码示例:

$("form").submit(function(e) {
    e.preventDefault();
    if ($('#email').val() == $('#cemail').val()) {
        var arr = [];
        arr["fname"] = $('#fname').val();
        arr["lname"] = $('#lname').val();
        arr["email"] = $('#email').val();
        arr["subject"] = $('#subject').val();
        arr["newsletter"] = $('#newsletter').val();
        arr["message"] = $('#message').val();

        $.ajax({
            url: "contact-ajax.php",
            method: "POST",
            data: {d: arr},
            success: function (d) {
                $('button#submit').css('background', '#A2D852');
                $('button#submit').html('Message Sent Successfully!');
                $('form').get(0).reset();
                //alert(d);
                console.log(arr);
                setTimeout( function(){                 
                    $('button#submit').css('background', '#FF8A00');
                    $('button#submit').html('Send Message <img src="contact/images/loading-icon.gif" id="loading-icon">');
                    $('#loading-icon').hide();
                },3000);
            },
            error: function(jqXHR, textStatus) {
                $('button#submit').css('background', '#F75D53');
                $('button#submit').html('Failed to send. Please try again!');
                setTimeout( function(){                 
                    $('button#submit').css('background', '#FF8A00');
                    $('button#submit').html('Send Message <img src="contact/images/loading-icon.gif" id="loading-icon">');
                    $('#loading-icon').hide();
                },4000);                    
            }
        });
    }
    else {
        alert("Your confirmation email does not match your email.");
        return false;
    }
});

var_dump($d)在控制台中返回null,但console.log(arr)返回有效数组。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

不使用数组,而是使用对象。

$("form").submit(function(e) {
    e.preventDefault();
    if ($('#email').val() == $('#cemail').val()) {

        // from this 
        // var arr = [];
        // arr["fname"] = $('#fname').val();
        // arr["lname"] = $('#lname').val();
        // arr["email"] = $('#email').val();
        // arr["subject"] = $('#subject').val();
        // arr["newsletter"] = $('#newsletter').val();
        // arr["message"] = $('#message').val();

        // to this :

        var dataObj = { fname: $('#fname').val(),
                        lname:  $('#lname').val(),
                        email: $('#email').val(),
                        subject: $('#subject').val(),
                        newsletter: $('#newsletter').val(),
                        message: $('#message').val()};


        $.ajax({
            url: "contact-ajax.php",
            method: "POST",
            data: dataObj,
            success: function (d) {
                $('button#submit').css('background', '#A2D852');
                $('button#submit').html('Message Sent Successfully!');
                $('form').get(0).reset();
                //alert(d);
                console.log(arr);
                setTimeout( function(){                 
                    $('button#submit').css('background', '#FF8A00');
                    $('button#submit').html('Send Message <img src="contact/images/loading-icon.gif" id="loading-icon">');
                    $('#loading-icon').hide();
                },3000);
            },
            error: function(jqXHR, textStatus) {
                $('button#submit').css('background', '#F75D53');
                $('button#submit').html('Failed to send. Please try again!');
                setTimeout( function(){                 
                    $('button#submit').css('background', '#FF8A00');
                    $('button#submit').html('Send Message <img src="contact/images/loading-icon.gif" id="loading-icon">');
                    $('#loading-icon').hide();
                },4000);                    
            }
        });
    }
    else {
        alert("Your confirmation email does not match your email.");
        return false;
    }
});

答案 1 :(得分:1)

如果您尝试用ajax发送数组,则不会在设置页面中发送

  var minprice = jQuery('#minPrice').val();
  var maxprice = jQuery('#maxPrice').val();
  var data =[];
  data['min'] = minprice;
  data['max'] = maxprice;

这不是像这样的数组。如果您以ajax发送此数组,则将像这样创建数组。

  

var data ={};

创建这样的数组

答案 2 :(得分:0)

$("#YourFormNameGoesHere").submit(function(e) {
    e.preventDefault();
    if ($('#email').val() == $('#cemail').val()) {
        var form_data = new FormData($('#YourFormNameGoesHere')[0]);
        $.ajax({
            url: "contact-ajax.php",
            method: "POST",
            data: form_data,
            async: true,
            cache:false,
            contentType: false,
            processData: false,
            success: function (d) {
                $('button#submit').css('background', '#A2D852');
                $('button#submit').html('Message Sent Successfully!');
                $('form').get(0).reset();
                //alert(d);
                console.log(arr);
                setTimeout( function(){                 
                    $('button#submit').css('background', '#FF8A00');
                    $('button#submit').html('Send Message <img src="contact/images/loading-icon.gif" id="loading-icon">');
                    $('#loading-icon').hide();
                },3000);
            },
            error: function(jqXHR, textStatus) {
                $('button#submit').css('background', '#F75D53');
                $('button#submit').html('Failed to send. Please try again!');
                setTimeout( function(){                 
                    $('button#submit').css('background', '#FF8A00');
                    $('button#submit').html('Send Message <img src="contact/images/loading-icon.gif" id="loading-icon">');
                    $('#loading-icon').hide();
                },4000);                    
            }
        });
    }
    else {
        alert("Your confirmation email does not match your email.");
        return false;
    }
});