I want to send data2 variable to my PHP script using but with below given code i am getting empty array of $_POST in my controller
var data2 = {
donation: $("#donationform").serialize(),
allocation: $("#allocationform").serialize(),
allocationerrors: { msg: validation() },
datatoform: new FormData(this)
};
$.ajax({
url: url,
type: "POST",
dataType: "JSON",
data: data2,
processData: false,
contentType: false,
success: function(data, status) {},
error: function(xhr, desc, err) {}
});
The problem is because you cannot send binary FormData in an object. You need to do it the other way around; ie. append the data to the FormData object and send that in the request. Try this:
var formData = new FormData(this);
formData.append('donation', $("#donationform").serialize());
formData.append('allocation', $("#allocationform").serialize());
formData.append('allocationerrors', JSON.stringify({ msg: validation() }));
$.ajax({
url: url,
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function (data, status) {
},
error: function (xhr, desc, err) {
}
});