There is a function that receives an array:

$(function() { $("#load").click(function(){ // Выполняем если по кнопке кликнули $.ajax({ url: "", // Обработчик type: "GET", // Отправляем методом GET data: {"num": num}, cache: false, success: function(response){ if(response == 0){ // Смотрим ответ от сервера и выполняем соответствующее действие alert('Записей нет!'); }else{ $("#comments").append(response); num = num + 1; } } }); }); }); 

The array content obtained by the var_dump function:

array (1) {[1] => array (5) {["Email"] => string (11) "admin @ admin" ["DateComment"] => string (10) "2016-05-04" [ "TimeComment"] => string (8) "01:41:28" ["Text"] => string (64) "Hello, I comment! Rporpopolpold" ["Mark"] => string (1) "5" }}

Question : how can I output the contents of this array in a loop to the page?

Instead of the contents of the array is written the word Array .

    2 answers 2

    First, you should specify the correct Content-type. Example php file:

     $array = [ [ 'Email' => 'admin@admin', 'DateComment' => '2016-05-04', 'TimeComment' => '01:41:28', 'Text' => 'Привет, я комментарий!рпорпоплполд', 'Mark' => '5' ] ]; header('Content-type: application/json'); // <---- вот эта строка echo json_encode($array); 

    Secondly, when ajax request, also specify the type of data requested:

      $(function() { $.ajax({ url: 'get.php', cache: false, dataType: 'json', // <---- вот здесь success: function(response) { $.each(response, function(i, el) { var model = '<div>Email: ' + el.Email + '</div>' + '<div>DateComment: ' + el.DateComment + '</div>' + '<div>TimeComment:' + el.TimeComment + '</div>' + '<div>Text: ' + el.Text + '</div>' + '<div>Mark: ' + el.Mark + '</div>'; $('#models_auto').append(model); }); } }); }); 
    • How do I display these values ​​in separate div ? I just get a string with values. - Denis
    • @ Denis combine 2 answers very difficult - Vasily Barbashev
    • @Denis updated the answer - Andrei Katalkin
    • @AndreiKatalkin thanks! the only adequate response in 2 days. - Denis

    You need to parse the response from the server using $ .parseJSON and, accordingly, must transfer from the server in the JSON format. Here an example is displayed here.

     function onAjaxSuccess(data) { $('#models_auto').html(''); var jsonObj = $.parseJSON(data); var html = ''; jsonObj.forEach(function(el){ html += '<option value='+el.id+'>'+el.name+'</option>'; }); $('#models_auto').append(html); } 
    • jsonObj.forEach is not a function error, how to deal? - Denis
    • and at you the array returns request? - gudfar
    • yes: {"1":{"Email":"admin@admin","DateComment":"2016-05-04","TimeComment":"01:41:28","Text":"\u041f\u0440\u0438\u0432\u0435\u0442, \u044f \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439!\u0440\u043f\u043e\u0440\u043f\u043e\u043f\u043b\u043f\u043e\u043b\u0434","Mark":"5"}} u0442, \ u044f \ u043a \ u043e \ u043c \ u043c \ u0435 \ u043d \ u0442 \ u0430 \ u0440 \ u0438 \ u0439! \ u0440 \ u043f \ u043e \ u0440 \ u043f \ u043e \ u043f \ {"1":{"Email":"admin@admin","DateComment":"2016-05-04","TimeComment":"01:41:28","Text":"\u041f\u0440\u0438\u0432\u0435\u0442, \u044f \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439!\u0440\u043f\u043e\u0440\u043f\u043e\u043f\u043b\u043f\u043e\u043b\u0434","Mark":"5"}} - Denis
    • @ Denis jsonObj that displays? - Vasily Barbashev
    • @ Vasily Barbashev `var jsonObj = JSON.parse (response); alert (jsonObj); `displays: Unexpected token o in JSON at position 1 - Denis