2

I'm building a contact form where when the user hits the submit button, it sends an email to the company from an ajax call. However, it is not passing the array variable from the form to the ajax PHP file. It seems to work when logging the array variable to the console upon success. But the data is missing from the email. Here is a sample of my code:

$("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) returns null in the console but console.log(arr) returns the valid array. Any help would be appreciated.

3
  • Try this: data: JSON.stringify({d: arr}) Commented Sep 3, 2015 at 18:17
  • @leo.fcx Still returns null. Commented Sep 3, 2015 at 18:19
  • Use the network tab of your browser's developer tools to inspect the request that's sent. You should be able to see the request payload that was (or wasn't) sent to your server. Commented Sep 3, 2015 at 18:28

3 Answers 3

3

Instead of using an array, use an object.

$("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;
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, this solved it. Making the array to an object actually worked.
@Daniel Great! Glad it helped!
2

if u try to send an array in ajax like this it's will be not sent in set page

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

this is array not declre like this. Array create like this if you send this array in ajax.

var data ={};

create array like this

1 Comment

var data ={}; This is declaring object, not array.
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;
    }
});                             

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.