Skip to content Skip to sidebar Skip to footer

Passing Array Php Into Json Gives Undefined

I want to pass a JSON from php to js. This is my php file : public function upload() { $record = 0; if (!empty($_FILES)) { $config['upload_path'] = './assets/uploa

Solution 1:

in php encode it like this

echo json_encode(array(
            "nama_csv" => $name_csv,
            "path" => $csv,
            "isi" => $tryOne
        ), JSON_FORCE_OBJECT);

In ajax you have to parse it. As you seem to be using jquery then you can do like this

data = jQuery.parseJSON(response);

and then you get array. Use that like data.isi or data['isi']. Not sure which one of this will work according to your array type.


Solution 2:

For the error after your update:

You are accessing the wrong index in your loop. See the corrected code:

var index = 1;
$.each(response.isi, function (i, item) {
$('#table-review').find('tbody').append("<tr>" +
    "<td>" + index + "</td>" +
    //"<td>" + item.i + "</td>" + // wrong: i doesn't exist as a property of item
    "<td>" + item[i] + "</td>" + // right: item array at index i will work
    "</tr>");
  index++;
  console.log(item);
});

Post a Comment for "Passing Array Php Into Json Gives Undefined"