I have a form with multiple values of the same name, and I need to arrange this data in an array before sending it via AJAX. However, when I try to do this using the .push function, I encounter an error that says "Uncaught TypeError: dArray.push is not a function."
var g = document.getElementsByName("dataA[]"); //equals 1, 11, 111
var dArray = [];
var eArray = [];
var fArray = [];
var gArray = [];
var hArray = [];
for(var i=0;i<g.length;i++){
dArray = d[i].value;
eArray = e[i].value;
fArray = f[i].value;
gArray = g[i].value;
hArray = h[i].value;
dArray.push(d[i].value); //Desired [1,11,111]
eArray.push(e[i].value);
fArray.push(f[i].value);
gArray.push(g[i].value);
hArray.push(g[i].value);
}
var dArrayb = JSON.stringify(dArray); //Desired ["1","11","111"]
var eArrayb = JSON.stringify(eArray);
var fArrayb = JSON.stringify(fArray);
var gArrayb = JSON.stringify(gArray);
var hArrayb = JSON.stringify(hArray);
var dataArray = "DataD="+ dArrayb +"&DataE="+ eArrayb +"&DataF="+ fArrayb +"&DataG="+ gArrayb +"&DataH="+ hArrayb +"";
alert(dataArray);
Desired Results
var dataArray = "DataD=["1","11","111"]"&DataE=["2","22","222"]"&DataF=["3","33","333"]"&DataG=["4","44","444"]"&DataH=["5","55","55"]"
Ajax send
$.ajax({
type: "POST",
url: "SaveData.php",
data: dataArray,
dataType: 'json',
async: false,
crossDomain: true,
cache: false,
success: function (output){
alert(output);
} //success response
}); //Close $.ajax