I am trying to make an array which then contains mutiple arrays (which are added in the each loop) and then pass the information to ajax to post off.
However, it won't add to the request, and when I try JSON.Stringify on the array, it just returns empty.
What am i doing wrong?
Thanks!
var graphics = new Array();
var customer = "Joe Bloggs";
var email = "[email protected]";
var contactnumber = "12345";
$('.graphic').each(function (i) {
var graphic = new Array();
graphic['name'] = $(this).attr('data-name');
graphic['type'] = $(this).attr('data-type');
graphic['price'] = $(this).attr('data-price');
graphic['model'] = $(this).attr('data-model');
graphic['carcolor'] = $(this).attr('data-carcolor');
graphic['graphiccolor'] = $(this).attr('data-color');
graphic['reg'] = $(this).attr('data-reg');
graphic['note'] = $(this).attr('data-note');
graphics.push(graphic);
});
$.ajax({
type: "POST",
data: {
customer:customer,
email:email,
contactnumber:contactnumber,
graphics:graphics
},
url: "assets/php/index.php",
success: function(msg){
$('.answer').html(msg);
}
});
var graphic = new Array()should bevar graphic = {}- otherwise it's not going to serialize correctly.graphicinside the each is an array, however you are treating ti as if it were an object. simply changenew Array()to{}and it will be more correct, however, still not in an acceptable format. To post that correctly you will need to serialize it as JSON after that.