0

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);  
    });
  }
);

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

It is actually not executing what i want although i get your idea. what is happening now is, the items keep repeating itself when i make the ajax request. so if i have john whenever i trigger the request, it produces duplicates of john with only one checkbox in the panel
I have updated my html as well. The checkbox only displays once and not for every item
0

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>');  
       });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.