I'm trying to create and populate a dynamic table using Javascript and JSON data fetched from a PHP script. The problem I'm encountering is that the first row (the headers) is created, and the keys from the JSON object are put in their correct place, but I can't create a second row with the values associated with those keys (Or it doesn't get displayed). Here is my JS code:
var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
var Json = JSON.parse(this.responseText);
for (value in Json.prods[0]){
alert(value);
}
var newTable = document.createElement('table');
//Create first row
var tableRowFirst = document.createElement('tr');
for (key in Json.prods[0]) {
//create new heading
var keys = document.createElement('th');
// append Heading to table
tableRowFirst.appendChild(keys);
//set new heading text content to json information
keys.textContent = key;
};
//Create rows
var tableBody = document.createElement('tbody');
var tableRow = document.createElement('tr');
tableBody.appendChild(tableRow);
for (key in Json.prods[0]) {
//create new cell
var values = document.createElement('td');
// append cell to row
tableRow.appendChild(values);
//set new content text content to json information
values.textContent = Json.prods[0].key;
};
//Append table to DOM
document.body.appendChild(newTable);
//Append rows to new table
newTable.appendChild(tableRowFirst);
newTable.appendChild(tableBody);
};
oReq.open("get", "../php/getalltag.php", true);
oReq.send();
Can anyone help me out?