user8120868
user8120868

Reputation:

Displaying all data from loop

I am able to fetch all my data from database successfully but only the last item in the array displays. What am I doing wrong?

HTML

@foreach($groups as $group)
<button type ="button" value="{!! $group->id !!}"  id="btn"  name="btn">{!!$group->name!!}</button>
    <div class="panel">
      <label for="myvalue"><input type="checkbox" id="myvalue" /> <span>Label text x</span></label>
    </div>
    @endforeach

JavaScript

$.ajax({
  type: "GET",
  url: "/dashboard/ajax=?id=" +id,
  data: {
    id: $(this).val(), 
    access_token: $("#access_token").val()   
  },
  success: function (result) { 
    $.each(result, function (i, fb) {
      $("label[for='myvalue']").text(fb.name);  
    });
  }
);

Upvotes: 0

Views: 55

Answers (2)

Nisal Edu
Nisal Edu

Reputation: 7591

You have to dynamically create new labels and add fb.name to it otherwise you will replace all values until the last value

success:function(result) { 
       $.each(result, function (i, fb) {
          $("#outerDiv").append('<label>'+fb.name+'</label>');  
       });
}

Upvotes: 0

aaron0207
aaron0207

Reputation: 2333

This way you are replacing the label text, not creating labels. What you are looking for would be something like:

<div class="panel" id="labels_cotainer">
  <label for="myvalue">
  <input type="checkbox" id="myvalue" />
  <span>Label text x</span></label>
</div>

 $.ajax({
    type: "GET",
    url: "/dashboard/ajax=?id=" +id,
    data:{ 
       id: $(this).val(), 
       access_token: $("#access_token").val()   
    },
    success:function(result) { 
       $.each(result, function (i, fb) {
          $("#labels_cotainer").append('<label>'+fb.name+'</label>');  
       }
    }
 });

This code will append every label to your panel

Upvotes: 1

Related Questions