I don't want unlike system my code is working on both like and unlike. anyone help me to sort out this I have try edit in JavaScript but i am beginner in JavaScript. If any one help me it would be appreciated.I have try to sort out this with if else but unable to debug..

 <?php 
      $status_query1 = "select * from  like_unlike WHERE userid= '$id' and profile_id= '$profile_id' and type ='1'";
        $status_result1 = mysqli_query($conn , $status_query1) or die(mysqli_error($conn));
            $status_row1 = mysqli_fetch_array($status_result1 , MYSQLI_ASSOC);
            $count_status1 = $status_row1['type'];

      if($count_status1 == NULL){
                $type2 = $status_row1['type'];                  
        ?>
                <input type="button" value="Like" id="like_<?php echo $profile_id; ?>" class="like" style="<?php if($type == 1){ echo "color: #ffa449;"; } ?>" />&nbsp;(<span id="likes_<?php echo $profile_id; ?>"><?php echo $total_likes; ?></span>)&nbsp;
     <?php
     }
     if($count_status1 == '1'){
     ?>

                    <input type="button" value="Unlike" id="unlike_<?php echo $profile_id; ?>" class="unlike" style="<?php if($type == 0){ echo "color: #ffa449;"; } ?>" />&nbsp;(<span id="unlikes_<?php echo $profile_id; ?>"><?php echo $total_unlikes; ?></span>)
                <?php   } ?>

/*Javascript */

$(document).ready(function(){

    // like and unlike click
    $(".like , .unlike").click(function(){
        var id = this.id;   // Getting Button id
        var split_id = id.split("_");

        var text = split_id[0];
        var profile_id = split_id[1];  // postid

        // Finding click type
        var type = 0;
        if(text == "like"){
            type = 1;
        }else{
            type = 0;
        }

        // AJAX Request
        $.ajax({
            url: 'likeunlike.php',
            type: 'post',
            data: {profile_id:profile_id,type:type},
            dataType: 'json',
            success: function(data){
                var likes = data['likes'];
                var unlikes = data['unlikes'];

                $("#likes_"+profile_id).text(likes);        // setting likes
                $("#unlikes_"+profile_id).text(unlikes);    // setting unlikes

                if(type == 1){
                    $("#like_"+profile_id).css("color","#ffa449");
                    $("#unlike_"+profile_id).css("color","lightseagreen");
                }

                if(type == 0){
                    $("#unlike_"+profile_id).css("color","#ffa449");
                    $("#like_"+profile_id).css("color","lightseagreen");
                }   
            },
            error: function(data){
                alert("error : " + JSON.stringify(data));
            }
        });    
    });       
});

1 Answers 11

from

// like and unlike click
$(".like , .unlike").click(function(){

to

// like and unlike click
$(".like").click(function(){

remove unlike class from click event

upvote
  flag
If user by mistake like and he want unlike then it is fixed only on like . user can't unlike ... i have remove unlike but when user want unlike his like post then its not working... – Arshad Khan
upvote
  flag
I have try to do this with if else logic in php but its not working.. – Arshad Khan
upvote
  flag
what actually you need when user hit unlike button.? – Abzkn
upvote
  flag
i want feature like facebook. when user hit unlike it will reset automatically. User first not unlike the button . First Button should be like if user like it status will be liked and user click again on button it status automatic should be like . For example facebook like – Arshad Khan

Not the answer you're looking for? Browse other questions tagged or ask your own question.