I ask for help. There are script posts, likes on it.

+1 happens by clicking <div id="like" data-id="'.$posting['id'].'">'.$posting['like'].'</div>. The problem is that +1 only happens on the last record from the foreach . The following ajax does not respond.

here is foreach php

  foreach ( $pjk as $posting ) { echo '<div class="col-auto" id="text_posting_user" data-id="'.$posting['id'].'" style="padding: 1.7em; padding-bottom: 0;"><div class="postings"><div class="bord-ava_post" style="background-image: url(' . $user_prof->avatar .');"></div><p><span style="font-weight: bold;">'.$posting['user_login'].'</span><span >X</span><br> <span class="datePost">'.$posting['dataname'].'</span> <span class="dateTime">'.$posting['time'].'</span></span></p> </div> <div class="text_p"><p>'.$posting['post'].'</p></div> <hr style="margin-bottom: 0.5rem;"> <div class="b_funk_post"> <div id="like" data-id="'.$posting['id'].'">'.$posting['like'].'</div> </div> <div> <textarea placeholder="ΠΠ°ΠΏΠΈΡˆΠΈΡ‚Π΅ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ..." style="margin-top: 4px;margin-bottom: 4px;border-radius: 30px;height: 35px;font-size: x-small;"></textarea> </div> </div>'; } 

and ajax

  $(document).ready(function() { $("#like").bind("click", function(event) { var dit = $(this).attr("data-id"); $.ajax({ url: "ajaxLike.php", type: "POST", data: ("id=" +dit), dataType: "text", success: function(result) { if (result) { $("#like").text(Number($("#like").text()) + 1); } else alert("Error"); } }); }); }); 

    1 answer 1

    The value of the id attribute must be unique. Use class

     <div class="like" data-id="'.$posting['id'].'">'.$posting['like'].'</div> $(document).ready(function() { $(".like").bind("click", function(event) { ...