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.
data: JSON.stringify({d: arr})