将按钮文本从“;喜欢“;至“;喜欢“;单击时


Change button text from "Like" to "Liked" when clicked

我正在创建一个论坛,用户可以在这里喜欢和不同的帖子和评论
我面临的挑战是,我希望在不重新加载页面的情况下更改点赞按钮的文本。我如何区分页面上的按钮,并将单个按钮文本从"赞"改为"赞",反之亦然
按钮目前已创建并显示如下:

<?php
$result3=mysqli_query($link, "SELECT * FROM cs_comments WHERE post_id = $postID");
while($row3 = mysqli_fetch_assoc($result3)){
    $commentID=$row3['comment_id'];
    $memberID=$row3['member_id'];
    $likes=$row3['likes'];
    $content= $row3['content'];
    $anonymous=$row3['anonymous'];

    if($anonymous=='1')
        $name='Anonym';
    else{
        $result4= mysqli_query($link, "SELECT firstname, lastname FROM cs_members WHERE member_id = $memberID");
        $row4=mysqli_fetch_assoc($result4);
        $name = '<a href="../profile/?profile=' . $memberID . '">' . $row4['firstname'] . " " . $row4['lastname'] . '</a>';
    }
    $result5=mysqli_query($link,"SELECT * FROM cs_likes WHERE comment_id=$commentID AND member_id={$_SESSION['memberID']}");
    if(mysqli_num_rows($result5)!=0)
        $liked="unlike";
    else
        $liked="like";


    echo '<div class="post_container">';
    echo '      <div class="info_header">';
    echo '          <div class="info_name">' . $name . '</div>';
    echo '          <div class="info_group"></div>';
    echo '      </div>';
    echo '      <div class="post_content">';
    echo            $content;
    echo '      </div>';
    echo '      <div class="post_footer">';
    echo '          <div class="like_button"><button onclick="likeComment('. $commentID . ')">' . $liked . '(' . $likes . ')</button></div>';
    echo '      </div>';
    echo '</div>';
}
mysqli_close($link);
?>

onclick事件的Javascript:

function likeComment(id) {
$.ajax({
    url: '/resources/phpScript/like.php',
    type: 'POST',
    data: {comment_id:id},
    success: function(data) {
        console.log(data); // Inspect this in your console
    }
});

Like.php

<?php
session_start();
include "./db-connect.php";
$memberID= $_SESSION['memberID'];
if(isset($_POST['post_id'])){
    $postID=mysqli_real_escape_string($link,$_POST['post_id']);
    $sqlCheck="SELECT * from cs_likes WHERE post_id = $postID AND member_id = $memberID";
    $sqlInsert="INSERT INTO cs_likes (post_id, member_id) VALUES ('$postID','$memberID')";
    $sqlDelete="DELETE FROM cs_likes WHERE post_id= $postID AND member_id = $memberID";
}
elseif(isset($_POST['comment_id'])){
    $commentID=mysqli_real_escape_string($link,$_POST['comment_id']);
    $sqlCheck="SELECT * from cs_likes WHERE comment_id = $commentID AND member_id = $memberID";
    $sqlInsert="INSERT INTO cs_likes (comment_id, member_id) VALUES ('$commentID','$memberID')";
    $sqlDelete="DELETE FROM cs_likes WHERE comment_id= $commentID AND member_id = $memberID";
}
else
    echo "Something went wrong";
$checkResult=mysqli_query($link, $sqlCheck);
if(mysqli_num_rows($checkResult)==0)
    $result=mysqli_query($link,$sqlInsert);
else
    $result=mysqli_query($link,$sqlDelete);
?>

感谢您的帮助!

所以我找到了解决方案!我对javascript几乎没有经验,这就是为什么这个问题可能太简单了,任何人都无法了解我在追求什么
这就是我所做的:

我只是通过在点击时向函数调用添加this来发送点击的元素:

echo '          <div class="like_button"><button onclick="likePost('. $postID . ',this)">' . $liked . '</button></div>';

然后我在我的函数中添加了这个小部分:

function likePost(id, elem) {
if(elem.innerHTML== "like")
    elem.innerHTML="unlike";
else
    elem.innerHTML="like";

我仍然要感谢所有为理解我的要求而付出努力的人。