I using serializeArray
to get all element and I am getting object like
[{name: "code[1][barcode]", value: "45534"},
{name: "code[1][rf_id]", value: "535353"},
{name: "code[1][serialize]", value: ""},
{name: "code[2][barcode]", value: "45534"},
{name: "code[2][rf_id]", value: "535353"},
{name: "code[2][serialize]", value: ""},
{name: "custodian[]", value: "3"},
{name: "custodian[]", value: "4"},
{name: "custodian[]", value: "5"}]
And I want to convert it like this
{
code:[
{barcode:"45534",rf_id:"535353",serialize:""},
{barcode:"45534",rf_id:"535353",serialize:""}
],
custodian: [3,4,5]
}
Currently I am using this script
var x = $('form#acquiredetail').serializeArray();
console.log(x);
var formData = {};
$.each(x, function(i, field){
if(field.value.trim() != ""){
formData[field.name] = field.value;
}
});
And getting output as
Although I am able to get value of code
properly but in backend/Laravel but the problem is with custodian
, I am getting last value,
custodian[]:"5"
How can I fix this. Or any better solution for this ?
My goal is to pass all element value to php with short and common code. Please suggest if any alternative of this.
Run the code snippet and enjoy :)