ginerjm Posted March 21, 2016 Share Posted March 21, 2016 With much help from Jacques and the forum I managed to get my php data into js with json and my appl now manages to do adds/deletes and the passing of results back to my php script to be updated into MySQL. All works great for existing data. My problem now is with the json part for entirely new records. When my php passes an empty array to json and to js and creates the web page, the very first add to the json object creates an object filled with null entries followed by the just-added piece of data. Alert messages tell me this: before the add, the item is (empty) and its type is object; after the add, the item becomes: ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,new, name and its type is still object. As you can see I am getting a series of null elements in my object but I dont' know from where they come. This doesn't happen (as I said) when working with existing data, only with new data records that have no json data to start with. My process is simply: atts = JSON.parse(document.getElementById('json_atts').innerText); and at startup an alert says it is empty and its type is object. To add: (before this add, atts is empty but of type object) atts[key] = membs[key]["name"]; after the above line atts is: ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Giner, Jim (a whole bunch of commas) What is causing this strange behavior? I am completely baffled by this because I'm a complete noob/incompetent at this json stuff. What I don't get is why this works for existing data that adds or deletes to the json-encoded data but the same logic won't work when the beginning data is empty. Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/ Share on other sites More sharing options...
Jacques1 Posted March 21, 2016 Share Posted March 21, 2016 (edited) Use console.log() instead of alert() for debugging. It will give you a detailed variable description in the JavaScript console. What's the “before” and the “after” now? How often is the atts[key] = membs[key]["name"]; line executed? Exactly once or in a loop? What are console.log(key) and console.log(membs[key]["name"]) ? The exact code would also be helpful. // And don't use innerText, because that's a nonstadard attribute which is actually wrong in this context. Use innerHTML. Edited March 21, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/#findComment-1532285 Share on other sites More sharing options...
ginerjm Posted March 21, 2016 Author Share Posted March 21, 2016 What is console.log? Is that js command? And what does it do? the add occurs one at a time.. The appl screen collects header rec data and then the individual names that need to be linked to that header data. I build the list of the details as the user does his d/e and then the submit passes the header data and all of the json-encoded names to PHP where the data is all posted into two tables. I have no html in the data.I'm building an object/array or the member names and their keys. Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/#findComment-1532286 Share on other sites More sharing options...
Jacques1 Posted March 21, 2016 Share Posted March 21, 2016 What is console.log? Is that js command? And what does it do? Console I have no html in the data.I'm building an object/array or the member names and their keys. I know, but the JSON data is HTML-escaped, so there are HTML entities which need to be translated. It looks like this is actually done by textContent, though. So either use textContent or the text() method of jQuery. Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/#findComment-1532290 Share on other sites More sharing options...
ginerjm Posted March 21, 2016 Author Share Posted March 21, 2016 I don't use jquery. Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/#findComment-1532291 Share on other sites More sharing options...
Jacques1 Posted March 21, 2016 Share Posted March 21, 2016 That's why I also mentioned textContent as a fallback. Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/#findComment-1532296 Share on other sites More sharing options...
ginerjm Posted March 21, 2016 Author Share Posted March 21, 2016 What again is wrong with my use of innerText? It gets me what I want. Again - all of my code seems to work just fine on any existing record data. It's only on new data that the screen breaks down. There must be some trick to properly initializing the empty array created in PHP and then being encoded to go to the javascript. Can I not simply define the empty array and pass it thru the same code that the populated one goes thru? Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/#findComment-1532301 Share on other sites More sharing options...
Jacques1 Posted March 21, 2016 Share Posted March 21, 2016 What again is wrong with my use of innerText? This: https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText Again - all of my code seems to work just fine on any existing record data. It's only on new data that the screen breaks down. There must be some trick to properly initializing the empty array created in PHP and then being encoded to go to the javascript. Can I not simply define the empty array and pass it thru the same code that the populated one goes thru? I don't think anybody can answer that question based on a single line of code. Show the complete code and the console.log() results. Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/#findComment-1532307 Share on other sites More sharing options...
ginerjm Posted March 22, 2016 Author Share Posted March 22, 2016 I'm getting close to a solution. Involves initializing my empty php data var when passing them to the script that json.parses them. Quote Link to comment https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/#findComment-1532335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.