Quantcast
Channel: Active questions tagged feed - Stack Overflow
Viewing all articles
Browse latest Browse all 542

problem to print result after ajax success

$
0
0

Please help to fix this issue with like counting.i create a news feed like facebook using php mysql and ajax.

problem is that when i click on like button it prints like count from (this value) and showing to all, let say i have 5 posts and like current value for different posts are 100, 200 , 70 , 80 , 578. when I click on first post ajax success count 100+1 = 101 for first post and for all other post printting same 101 likes. now if i will go to 2nd post and its like value 200, ajax will show 200+1 =201 like. so after click on 2nd post all 5 posts like will show 201. its creating problem for me. I understand that after ajax success i mentioned to show the value to div class (.ajax_like_result), thats why its showing in every post, same result until i am not clicking on different post.
how to fix this so that when I click on any post it wil show only its real like value??

I try to change the DIV attribute id , instead of class then it only works for first post. other post totally not working. if i set div atribute to class then like is working but printing incorrectly as i mentioned above.

I have pasted below- html , php and ajax code . thanks for your help.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script type="text/javascript">$(document).ready(function(){            $('.ajax_like').on('click', function () {        var post_id = $(this).val();         var page = $(this).attr("data-id");         $.ajax({           type: 'POST',           url: 'like.php',           data: {post_id:post_id, page:page},       dataType: 'json',                // cache: false,           success: function(data){                                $(".ajax_like_result").html(data);                              }        });    });});</script>html : <li class="ajax_like" type="submit"  name="data-id" data-id="<?php echo $page;?>" value="<? echo $post_id;?>">                         <div class= "ajax_like_result" ><?php print $likes;?></div></li>like.php code :<?php //user info startsession_start();$conn=mysqli_connect("localhost", "u1068033_ab24", "ab@24", "u1068033_ab24");mysqli_set_charset($conn,"utf8");$id=$_SESSION['id'];$get_post_id = $_POST['post_id'];$page = $_POST['page'];$sql="SELECT `likes` FROM $page WHERE `post_id`='$get_post_id'";$query=mysqli_query($conn,$sql);$row=mysqli_fetch_assoc($query);$likes=$row['likes'];$sql="UPDATE $page SET `likes`=$likes+1 WHERE `post_id`='$get_post_id'";$query=mysqli_query($conn,$sql);$sql="SELECT `likes` FROM $page WHERE `post_id`='$get_post_id'";$query=mysqli_query($conn,$sql);$row=mysqli_fetch_assoc($query);echo $row['likes'];`enter code here`?>

Viewing all articles
Browse latest Browse all 542

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>