I have a JavaScript array that is constantly updated.
Here's how I initially set up the array...
columnArray = ["userID", "Date", "trialType", "cue", "target", "response", "accuracy", "lenAcc", "strictAcc", "fkpl", "totalTime"];
var dataArray = [];
dataArray.push(columnArray);
Later on, I modify the data based on various events and calculations...
dataArray.push(userID, new Date(), trialType, cue, target, response, accuracy, lenAcc, strictAcc, fkpl, totalTime);
When displaying the array using this code...
document.getElementById("trial").innerHTML = dataArray;
...everything appears neat with line breaks for readability:
userID,Date,trialType,cue,target,response,accuracy,
lenAcc,strictAcc,fkpl,totalTime,
Ad31,Mon Dec 18 2017 11:35:55 GMT-0500 (Eastern Standard
Time),copy,hello,world,xzzvxzxvxzxcv,0,0,0,938,5001
However, when I attempt to save the data using this function...
function saveData(){
$.ajax({
type: "POST",
url: "saveData.php",
dataType: "application/json",
ContentType: "application/json; charset=utf-8",
data: {"data": JSON.stringify(dataArray)},
})
}
...only the header information is sent (again with line breaks):
[["userID","Date","trialType","cue","target","response","accuracy",
"lenAcc","strictAcc","fkpl","totalTime"]]
I read somewhere that perhaps my array should be an object instead, so I tried...
var dataArray = {};
...instead of...
var dataArray = [];
But will I still be able to use dataArray like a regular array if I make this change (e.g., push items to the end, store large amounts of data, etc.)?
Or could there be another underlying issue causing this problem?
My ultimate goal is to have each "trial" represented on a single line in the array, for better formatting. But for now, I just want to ensure the data is correctly saved.
Thank you!
SOLVED! The issue has been resolved. The problem was simply that I was calling my Ajax save function BEFORE appending the data to the array with push(). Once I rearranged my save function to execute right after pushing the data into the array, the file gets saved with all essential details intact!