单击时将按钮文本从“赞”更改为“喜欢”

时间:2016-05-05 00:59:08

标签: javascript php

我正在创建一个用户可以喜欢的论坛,而不像帖子和评论。
我面临的挑战是,我希望在不重新加载页面的情况下更改like按钮的文本。如何区分页面上的按钮并将单个按钮文本从“喜欢”更改为“喜欢”,反之亦然?
按钮在此刻创建并显示如下:

<?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-event的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);
?>

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

所以我找到了解决方案!我几乎没有使用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";

我仍然要感谢所有努力尝试理解我所要求的人。