채택완료

어느부분이 잘못된건지 모르겠습니다

2022-08-05 (금) 12:28:55 4,211
Copy
<?php

 

$sql = " SELECT * FROM g5_board_good WHERE mb_id='{$member['mb_id']}' AND bo_table='${$bo_table}' AND bg_flag='good' AND wr_id='$list[$i]['wr_id']' ";

$memberGood = sql_fetch($sql);

 

 

  if ($memberGood) {

    echo "<a href=\"".$good_href."\"  id=\"lt_good\" class=\"lt_v_good\">" . "<span class=\"lt_good\">" . " <i class=\"fa fa-heart\"></i><strong>{$list[$i]['wr_good']}</strong>". "</span>";        

    echo "<b class=\"lt_v_act_good\"></b>";

    echo "</a>";

    // 내가 좋아요를 눌렀을때 출력

  } else {

    echo "<a href=\"".$good_href."\"  id=\"lt_good\" class=\"lt_v_good\">" . "<span class=\"lt_good\">" . " <i class=\"fa fa-heart-o\"></i><strong>{$list[$i]['wr_good']}</strong>". "</span>";        

    echo "<b class=\"lt_v_act_nogood\"></b>";

   echo "</a>";

    // 내가 좋아요를 누르지 않았을때 출력

  }

 

?>

<!-- ---------------------------------------------------------------- -->


 

<?php }  ?>

 

<script>

$(function() {

    // 추천, 비추천

    $(".lt_v_good").click(function() {

        $tx = $(this).children(".lt_v_act_good");

 

 

        excute_good(this.href, $(this), $tx);

        return false;

    });

});

 

// $(function() {

//     // 추천, 비추천

//     $(".lt_v_good , .lt_v_nogood").click(function() {

 

//         if($memberGood){

//         $tx = $(this).children(".lt_v_act_good");

//         }

//         else{

//         $tx = $(this).children(".lt_v_act_nogood");

//         }

//         excute_good(this.href, $(this), $tx);

//         return false;

//     });

// });

 

</script>

 

<script>

function excute_good(href, $el, $tx)

{

    $.post(

href,

        { js: "on" },

        function(data) {

if(data.error) {

alert(data.error);

                return false;

            }




 

if(data.count) {

    $el.find("strong").text(number_format(String(data.count)));

}

 

if(data.msg){

    $tx.stop().hide();

    $tx.text(data.msg);

    $tx.fadeIn(200).delay(2500).fadeOut(200);

}



 

        }, "json"

    );

}

</script>

좋아요를누르면 하트가 아니면 흰색하트가 나오도록 하고싶은데 눌러도 숫자만 바뀌고 하트는 바뀌지 않습니다 어느부분이 잘못된건지 알려주세요!

991484400_1659670108.0936.png

답변 2개 / 댓글 4개

채택된 답변
+20 포인트
2022-08-05 (금) 15:30:58

if (data.count) {
    $el.find("strong").text(number_format(String(data.count)));
}

->

if (data.count) {
    $el.find("strong").text(number_format(String(data.count)));
    $el.find("i").removeClass('fa-heart-o').addClass('fa-heart');
}

답변에 대한 댓글 3개

아래의 방법으로 수정해보겟습니다
아래꺼로 바꾸었을 때 하트는 색이 들어가나 다시 추천취소를하면 하트가 색이 변하지 않고 그대로 빨간하트로 남아있습니다 혹시 어느 부분에서 막힌걸까요?
2022-08-08 (월) 14:17:46
로직상으로,

추천일 때
-> $el.find("i").removeClass('fa-heart-o').addClass('fa-heart');

취소일 때
-> $el.find("i").removeClass('fa-heart').addClass('fa-heart-o');

식으로 처리하면 됩니다.

코드상에서 '추천 취소' 부분이 어디인지.. 저는 찾지 못했습니다.

위의 php 조건문은 최초 페이지를 로딩할때만 작동됩니다.
좋아요 이벤트는 ajax로 이루어져있으니 ajax 응답값을 받아 처리하는 excute_good 함수에서도 별도의 처리를 해주셔야 합니다

답변에 대한 댓글 1개

ajax에 기본적인 배경이 없어서 어떤식으로 만들어야하는지 알려주실수 있을까요?

답변을 작성하려면 로그인이 필요합니다.