Javascript json_encode > JSON.parse() array

Viewed 81874

I the following PHP code:

echo "<img src='../images/edit.png' onclick='showEditDiv(" . json_encode($row) . ");'>";

Here is the HTML result:

<img src='../images/edit.png' onclick='showEditDiv({"ID":"2","NAME":"John Smith","EMAIL":"johnsmith@domain.com"});'>

And here is the Javascript code:

function showEditDiv(data) {
  alert(data);
  data = JSON.parse(data);
  alert(data);
  for (i=0; i< data.length; i++){ 
    alert(data[i]);
  };
}

The problem is I'm not getting the desired array in the JS parameter. The first alert shows "[object Object]" and that's all, no more alerts. Where is the problem? My code is based on the examples I found here. All I want is to pass an array to JS function, which is in a separated .js file. I don't want to use JQuery, prefer native JS.

4 Answers
Related