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>
.html(data[0].name)... Do you know how to access JSON data?$('.result').append('<div>' + data[0].name + '</div>')- it add div with name of first result to div with result class.success: function(data){ var newhtml = ''; $.each(data, function(i, item) { newhtml +='<div>'+ item.name +'</div>'; }); $('.result').html(newhtml); }