I looked at Iterate through nested json object array and some other posts, but they don't seem to be explaining my situation.
The issue I am having here is that I am having to store multiple JSON objects into an array to local storage in the event the client's form goes offline and needs to store multiple submissions into local storage; so when the connection is re-established, it will iterate through all the localstorage data and submit each JSON object separately into new rows into the SQL DB.
All I have are arrays of JSON objects being stored into a localstorage array, but I can't seem to return each "{...}" object separately, I can only get a single value of the array string using offlinExtractData[3] (returns "c" of "cable_no") instead of being able to get the first JSON Object in the array of multiple objects.
Example, a complete JSON object that will submit to the SQL DB looks like this, which is then sent to PHP SQL file via AJAX:
{"cable_no":"90012","section_no":"1","extract_ft":"1","cable_status":"","conduit_status":"None","extract_date":"2013-09-26","extraction_team":" ","extract_notes":"1"}
That is one row of data, but this is what my local storage will look like after multiple form submissions:
[{"cable_no":"90012","section_no":"1","extract_ft":"1","cable_status":"Done","conduit_status":"None","extract_date":"2013-09-26","extraction_team":" ","extract_notes":"1"},
{"cable_no":"90012","section_no":"1","extract_ft":"2","cable_status":"Done","conduit_status":"Liner 2","extract_date":"2013-09-26","extraction_team":"E1","extract_notes":"2"},
{"cable_no":"90012","section_no":"1","extract_ft":"3","cable_status":"Done","conduit_status":"Liner 2","extract_date":"2013-09-26","extraction_team":"Unknown","extract_notes":"3"},
{"cable_no":"90012","section_no":"4","extract_ft":"4","cable_status":"Done","conduit_status":"Liner 2","extract_date":"2013-09-26","extraction_team":"Unknown","extract_notes":"4"},
{"cable_no":"90012","section_no":"1.2.3","extract_ft":"333","cable_status":"Done","conduit_status":"Liner 2","extract_date":"2013-09-26","extraction_team":"Unknown","extract_notes":"5"}]
each {...},{...},{...}
JSON Object has its own unique row of data, and I need to be able to return each {...}
accordingly and separately to submit through my AJAX function which I already have set up. My question is how can I iterate through each object in order to return the whole piece of data, not just bits of the whole string? Does that make sense?
Here is the code that spits out the above data:
//Global variables
var extractform = {
'cable_no' : "",
'section_no' : "",
'extract_ft' : "",
'cable_status' : "",
'conduit_status' : "",
'extract_date' : "",
'extraction_team' : "",
'extract_notes' : ""
}
//initially set up the store
if (!localStorage["Extract_Update"] ) {
localStorage["Extract_Update"] = JSON.stringify([]);
}
//Now return locally stored values, if set
if (localStorage["Extract_Update"]) {
var offlinExtractData = localStorage["Extract_Update"];
console.log("Array Length: " + localStorage["Extract_Update"].length + "\nParsed Data: " + offlinExtractData);
console.log(offlinExtractData[3]);
}
function update_extractform() {
$.ajax({
type: 'POST',
//url: './php/update_extractform.php',
timeout: 8000, //8 seconds
data: extractform,
success: function(data) {
//Save data (array) and push new data into existing web storage
var aa = JSON.parse( localStorage["Extract_Update"] );
console.log( aa );
aa.push( [ extractform ] );
localStorage["Extract_Update"] = JSON.stringify( aa );
console.log("Submitted (offline): " + localStorage["Extract_Update"]);
}