0

I would like to create a like button using PHP, MySQL and jQuery, but seems there's an error, i don't know where is it, can you help ?

I have two pages [index.php & callback.php]

INDEX

$k = 1; //POST ID

$nip = 24; //USER ID

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $(document).on('click', '.like', function(){
            if($(this).attr('title')=='Like'){
                $.post('callback.php',{k:$(this).attr('id'),action:'like'},function(){
                    $(this).text('Unlike');
                    $(this).attr('title','Unlike');
                });
            }else{
                if($(this).attr('title')=='Unlike'){
                    $.post('callback.php',{k:$(this).attr('id'),action:'unlike'},function(){
                        $(this).text('Like');
                        $(this).attr('title','Like');
                    });
                 }
             }
        });
    });
    </script>
</head>
<body>

<?php
$query=$db->prepare("SELECT * FROM activity WHERE nip = :nip AND value = :value");
$query->execute(array(
    ':nip'      => $meNip,
    ':value'    => $k
    )
);
$all = $query->rowCount();
if($query->rowCount()==1){
    echo '<a href="#" class="like" id="'.$k.'" title="Unlike">Unlike</a> <b>'.$all.'</b>';
}else{
    echo '<a href="#" class="like" id="'.$k.'" title="Like">Like</a> <b>'.$all.'</b>';
}
?>
</body>
</html>

CALLBACK

$nip= //COOKIE

$k= $_POST['k'];

$action=$_POST['action'];

if (!empty($k)) {
    $checkAd=$db->prepare("SELECT * FROM ad WHERE id = :id");
    $checkAd->execute(array(
        ':id'   => $k
        )
    );
    $checkingAd=$checkAd->fetchAll(PDO::FETCH_ASSOC);
    foreach ($checkingAd as $row) {

    //LIKE
    if ($action=='like'){
        $callback=$db->prepare("SELECT * FROM activity WHERE nip = :nip AND value = :value");
        $callback->execute(array(
            ':nip' => $meNip,
            ':value' => $k
            )
        );
        $matches=$callback->rowCount();
        if($matches==0){
        $callback=$db->prepare("UPDATE ad SET likes = :likes WHERE id = :id");
        $callback->execute(array(
            ':likes'    => $row['likes']+1,
            ':id'   => $k
            )
        );
        $callback=$db->prepare("INSERT INTO activity (nip, value) VALUES(:nip, :value)");
        $callback->execute(array(
            ':nip'  => $meNip,
            ':value' => $k
            )
        );
    }
    }elseif ($action=='unlike'){ //UNLIKE
        $callback=$db->prepare("SELECT * FROM activity WHERE nip = :nip AND value = :value");
        $callback->execute(array(
            ':nip'  => $meNip,
            ':value' => $k
            )
        );

        $matches=$callback->rowCount();
        if($matches==1){
            $callback=$db->prepare("UPDATE ad SET likes = :likes WHERE id = :id");
            $callback->execute(array(
                ':likes' => $row['likes']-1,
                ':id'   => $k
                )
            );
            $callback=$db->prepare("DELETE FROM activity  WHERE nip = :nip AND value = :value");
            $callback->execute(array(
                ':nip'  => $meNip,
                ':value' => $k
                )
            );
            }
        }
    }
 }
 ?>

I tested the callback.php file, (address bar using GET) it's working fine, can you check the INDEX, i think, i miss something, a dot ?

Thanks for your help

10
  • What error do you get? Commented Feb 20, 2014 at 13:56
  • what is the issue you're facing? Like/Unlike aren't being registered in the DB? Are you sure this if is working: if($(this).attr('title')=='Like'){? And did you check your browser's console to see what data is being sent to callback.php Commented Feb 20, 2014 at 13:57
  • @tftd Like/Unlike don't work, nothing's posting to callback.php file Commented Feb 20, 2014 at 14:00
  • is there a post at all? Commented Feb 20, 2014 at 14:02
  • you cant create a button using php and mysql. have to use frontend lang :D Commented Feb 20, 2014 at 14:04

1 Answer 1

2

Your reference to $(this) in $.post is incorrect. You probably assumed $(this) will be the .like element, but that's not the case. this in a $.post would return something similar:

Object { readyState=1, getResponseHeader=function(), getAllResponseHeaders=function(), more...}
Object { url="callback.php", type="POST", isLocal=false, more...}

The code below should change the text accordingly.

<script type="text/javascript">
$(document).ready(function(){
    $(document).on('click', '.like', function(){
        var $this = $(this);
        var likes_count = $('.likes_count');
        if($(this).attr('title')=='Like'){
            $.post('callback.php',{k:$(this).attr('id'),action:'like'},function(){
                $this.text('Unlike');
                $this.attr('title','Unlike');
                var likes = parseInt(likes_count.text())+1;
                likes_count.text(likes);
            });
        }else{
            if($(this).attr('title')=='Unlike'){
                $.post('callback.php',{k:$(this).attr('id'),action:'unlike'},function(){
                    $this.text('Like');
                    $this.attr('title','Like');
                    var likes = parseInt(likes_count.text())-1;
                    likes_count.text(likes);
                });
             }
         }
    });
});
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Unlike & Like (they are for the title, not for callback) as you see ($.post('callback.php',{k:$(this).attr('id'),action:'like'},function(){) a lower case...
You're right, my mistake. Check if the updated answer works for you.
Cool, but the affected rows ($all) still the same, when i click on LIKE 23, i see UNLIKE 23, should be UNLIKE 24 right ? echo '<a href="#" class="like" id="'.$k.'" title="Like">Like</a> <b>'.$all.'</b>'; Is there a way to echo $all+1
@Simdine I've updated my answer. This should work, but have in mind you might need to make some changes to get it to work if you have multiple pages to like and multiple like counters for every page. By the way, when you ask a question here on SO, please tell what your code should be doing in the question so that people would be able to better understand what you want to do. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.