mardi 20 juillet 2021

Ajax send data to the database error while using php while

I have a big problem in my php code. I create a social network. In that social networking site, people can post things and others can like them. But my problem is related to the 'likes' part. Here is my php code that i use to get the post data from the database

<?php  
$select1 = mysqli_query($con, "SELECT * FROM `newtons` WHERE by_user='$user_name' ORDER BY `newtoned_at` DESC ") ;
while ($row1 = mysqli_fetch_array($select1)) {
  $newton_id = $row1['id'];
  $text = $row1['text'];
  $picture = $row1['picture'];
  $link = $row1['link'];
  $hashtag = $row1['hashtag'];
  $newtoned_at = $row1['newtoned_at'];

  echo "

<div class='m-1 border p-2' id='newton_display_box'>

<div class='d-flex flex-row'>

<div><img src='profile_pictures/$profilepic' class='rounded border' style='height:50px;width:50px;'></div> 

<div class='ml-1'>
<p class='h6 text-secondary'>@$user_name</p>
<p class='h6' id='newton_display_box_text'>$text</p>
</a>
</div>
</div>

**<form method='post' action=''>

<input type='hidden' id='by_user' value='$username'>
<input type='hidden' id='to_newton' value='$newton_id'>
<input type='hidden' id='to_user' value='$user_name'>


<div class='row text-center mt-1' style='font-size:20px;'>
<div class='col-4'> <button class='far fa-heart reactions' id='likebtn'></button> </div>
<div class='col-4'> <button class='far fa-comment-alt reactions'></button> </div>
<div class='col-4'> <button class='fas fa-grin-hearts reactions'></button> </div>
</div>
</form>**

</div>

I hope you can clearly see the last part which is a form that i used to send data to the database using this ajax code,

<script>
  $(document).ready(function(){
    $('#likebtn').click(function(event){
      event.preventDefault();
      var by_user = $('#by_user').val();
      var to_newton = $('#to_newton').val();
      var to_user = $('#to_user').val();
      $.ajax({
          type: "POST",
          url: "process_like.php",
          data: { by_user:by_user, to_newton:to_newton, to_user:to_user},       
          dataType: "json",

      });
    });
  });
</script>

But friends my problem is i can only send the data of the 1st post! I'm unable the send of the other posts to the database! Please help me. I think this is a problem as i used the php while. Please help me




Aucun commentaire:

Enregistrer un commentaire