Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/301058-json-handling-with-no-starting-data/
Share on other sites

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 by Jacques1

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.

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.

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?

What again is wrong with my use of innerText?

 

This:

post-167590-0-60432800-1458594320_thumb.png

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.