I am working on a site that has like button attached to posts made by users. I want whenever a user likes a post, the like count of that post should be replaced with current one but it is affecting the whole post.
This is code for the Like button
echo "<span class = 'likecount'>". $likes . "</span><button class='mlike pacedown'
value='".$post_id."' name = 'like' type='submit'><span class = 'buttons'>Like</span>
<span class='glyphicon glyphicon-heart'></span></button>";
And this the AJAX that gets fired whenever the button is clicked:
$(".mlike").click(function () {
$(".murconform").submit(function(e){
return false;
});
var post_id = $(this).val();
var user_id = $(".user_id").text();
var request = $.ajax({
url: "likes.php",
type: "POST",
data: { post : post_id , user : user_id },
dataType: "html"
});
request.done(function( msg ) {
alert ("User ID is " + user_id + " Post ID is " + post_id);
$('.likecount').html( msg );
});
});
And this is the echo
result from likes.php
after update the database:
echo "<span class = 'likecount'>". $count . "</span>";
The database side is working just fine.