i want add user like to post on page load i.e i want to make the like botton show liked if user already liked the post but it not working this is my ajax code
for (var l = 0; l < 5; l++) {
this_comment_id = $('#like_check_'+l).attr('data-id');
$.ajax({
url:"comment_post.php",
type: 'POST',
data:{comment_check:this_comment_id},
dataType: 'text',
success:function(data)
{
if (data.search("ok") > -1) {
$('#like_check_'+l).addClass("red_heart");
}else if(data.search("unliked") > -1){
$('#like_check_'+l).removeClass("red_heart");
}
}
})
}
it is meant to add the red_hearth to liked post by the user in this html element
<i onclick="like_post(this)" class="fas fa-heart float-right p-1 my-1 mr-3" data-id="1" id="like_check_0" data-toggle="tooltip" data-placement="top" title="I like it"></i>
<i onclick="like_post(this)" class="fas fa-heart float-right p-1 my-1 mr-3" data-id="2" id="like_check_1" data-toggle="tooltip" data-placement="top" title="I like it"></i>
<i onclick="like_post(this)" class="fas fa-heart float-right p-1 my-1 mr-3" data-id="3" id="like_check_2" data-toggle="tooltip" data-placement="top" title="I like it"></i>
<i onclick="like_post(this)" class="fas fa-heart float-right p-1 my-1 mr-3" data-id="4" id="like_check_3" data-toggle="tooltip" data-placement="top" title="I like it"></i>
<i onclick="like_post(this)" class="fas fa-heart float-right p-1 my-1 mr-3" data-id="5" id="like_check_4" data-toggle="tooltip" data-placement="top" title="I like it"></i>
but it is not working this is my php code for the ajax request
if (isset($_POST['comment_check'])) {
$get_comment_id = mysqli_real_escape_string($con, test_input($_POST['comment_check']));
$get_user_id = $_SESSION['user_id'];
$commentcheck = mysqli_query($con, "SELECT * FROM `comment_like` WHERE `liker_id` = '$get_user_id' AND`comment_id` = '$get_comment_id' AND liked='Y'") or exit(mysqli_error($con));
if(mysqli_num_rows($commentcheck)>0){
echo "ok";
}else{
echo "unliked";
}
}