最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Displaying php result in HTML using AJAX - Stack Overflow

programmeradmin2浏览0评论

I am trying to display the result of my php in different <div>-s. My plan is that I make a query in my php and display the result in JSON format. Due to the JSON format my result can be displaying in different <div>. How can I reach that for example the "name" can be displayed between <div> tags?

The example result of php:

[
    {
        "id": 0,
        "name": "example1",
        "title": "example2"
    },
    {
        "id": 0,
        "name": "example1",
        "title": "example2"
    }
]

The attempt:

 <div class="result"></div>

 <script>
 $.ajax({
    type:'GET',
    url:'foo.php',
    data:'json',
    success: function(data){
            $('.result').html(data);
    }
});
</script>

I am trying to display the result of my php in different <div>-s. My plan is that I make a query in my php and display the result in JSON format. Due to the JSON format my result can be displaying in different <div>. How can I reach that for example the "name" can be displayed between <div> tags?

The example result of php:

[
    {
        "id": 0,
        "name": "example1",
        "title": "example2"
    },
    {
        "id": 0,
        "name": "example1",
        "title": "example2"
    }
]

The attempt:

 <div class="result"></div>

 <script>
 $.ajax({
    type:'GET',
    url:'foo.php',
    data:'json',
    success: function(data){
            $('.result').html(data);
    }
});
</script>
Share Improve this question edited Jun 13, 2017 at 13:27 Death-is-the-real-truth 72.3k10 gold badges57 silver badges104 bronze badges asked Jun 13, 2017 at 13:10 bummm26bummm26 1595 silver badges17 bronze badges 3
  • .html(data[0].name)... Do you know how to access JSON data? – Jeremy Thille Commented Jun 13, 2017 at 13:13
  • 1 $('.result').append('<div>' + data[0].name + '</div>') - it add div with name of first result to div with result class. – guest Commented Jun 13, 2017 at 13:14
  • success: function(data){ var newhtml = ''; $.each(data, function(i, item) { newhtml +='<div>'+ item.name +'</div>'; }); $('.result').html(newhtml); } – Death-is-the-real-truth Commented Jun 13, 2017 at 13:15
Add a ment  | 

3 Answers 3

Reset to default 4

Hi try parsing the returned data into a JSON object :

<div class="result"></div>

 <script>
 $.ajax({
    type:'GET',
    url:'foo.php',
    data:'json',
    success: function(data){

            // added JSON parse
            var jsonData = JSON.parse(data);

            // iterate through every object
            $.each(jsonData, function(index, element) {

                 // do what you want with each JSON object
                 $('.result').append('<div>' + element.name + '</div>');

            });                
    }
});
</script>

// PS code is untested. // Regards

Do it like below:-

success: function(data){
    var newhtml = ''; //newly created string variable
    $.each(data, function(i, item) {//iterate over json data
        newhtml +='<div>'+ item.name +'</div>'; // get name and wrapped inside div and append it to newly created string variable
    });
    $('.result').html(newhtml); // put value of newly created div into result element
}

You can create it like this:

for (var i = 0; i < res.length; i++) {
  var id = $("<div></div>").html(data[i].id);
  var name = $("<div></div>").html(data[i].name);
   var title = $("<div></div>").html(data[i].title);

  $('.result').append(id).append(name).append(title);
}
发布评论

评论列表(0)

  1. 暂无评论