jquery - Like & unlike voting system with PHP issue

574

I have a profile page and under my photo i have a like button with a counter and i want when i hit like, the text for like button to be setted with "unlike" but the counter to stay there with all like's that profile page have from all the users & when i hit unlike to take my like back.

I'm begginer and i messed-up with the code, so please help, i will apreciate,thanks!

This is what i have so far: likes.js

$(document).ready(function(){
    $(document).on('click', '.like', function(){
        if($(this).attr('title') == 'Like'){
            $that = $(this);
            $.post('action.php', {id:$(this).attr('id'), action:'like'},function(){
            $that.text('Unlike');
            $that.attr('title','Unlike');
            });
        }else{
            if($(this).attr('title') == 'Unlike'){
                $that = $(this);
                $.post('action.php', {id:$(this).attr('id'), action:'unlike'},function(){
                    $that.text('Like');
                    $that.attr('title','Like');
                });
            }
        }
    });
});

Php count process file:

<?php
require_once "config.php";
$id=$user->filter->id; //User id
$sql=$dbh->prepare("SELECT * FROM likes WHERE id=?");
$sql->execute(array($id));
if($sql->rowCount()==1){
    echo '<div class='btn btn-warning like' id="'.$id.'" title="Unlike" style='margin-top: -6px; title="Like"'> Like <span class='like-count'>0</span></div>';
}else{ 
    echo '<div class='btn btn-warning like' title="Like" style='margin-top: -6px;'> Like <span class='like-count'>0</span></div>';
}
?>

action.php file:

<?php
require_once "config.php";

$id=$user->filter->id;
$action=$_POST['action'];
if ($action=='like'){
    $sql=$dbh->prepare("SELECT * FROM likes WHERE id=?");
    $sql->execute(array($id));
    $matches=$sql->rowCount();
    if($matches==0){
        $sql=$dbh->prepare("INSERT INTO likes (id) VALUES(?)");
        $sql->execute(array($id));
        $sql=$dbh->prepare("UPDATE likes SET likes=likes+1 WHERE id=?");
        $sql->execute(array($id));
    }else{
        die("There is No profile With That ID");
    }
}
if ($action=='unlike'){
    $sql = $dbh->prepare("SELECT 1 FROM `likes` WHERE id=?");
    $sql->execute(array($id));
    $matches = $sql->rowCount();
    if ($matches != 0){
        $sql=$dbh->prepare("UPDATE likes SET likes=likes-1 WHERE id=?");
        $sql->execute(array($id));
    }
}
?>

.like-count {
    background-color: black;
    padding: 2px 12px 2px;
    margin-left: 2px;
    border-bottom: 1px solid;
    color:white;
}

.btn {
    display: inline-block;
    padding: 4px 12px;
    margin-bottom: 0px;
    font-size: 14px;
    line-height: 20px;
    color: #333;
    text-align: center;
    text-shadow: 0 1px 1px rgba(255,255,255,0.75);
    vertical-align: middle;
    cursor: pointer;
    background-color: #f5f5f5;
    background-image: -moz-linear-gradient(top,#fff,#e6e6e6);
    background-image: -webkit-gradient(linear,0 0,0 100%,from(#fff),to(#e6e6e6));
    background-image: -webkit-linear-gradient(top,#fff,#e6e6e6);
    background-image: -o-linear-gradient(top,#fff,#e6e6e6);
    background-image: linear-gradient(to bottom,#fff,#e6e6e6);
    background-repeat: repeat-x;
    border: 1px solid #ccc;
    border-color: #e6e6e6 #e6e6e6 #bfbfbf;
    border-color: rgba(0,0,0,0.1) rgba(0,0,0,0.1) rgba(0,0,0,0.25);
    border-bottom-color: #b3b3b3;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffffff',endColorstr='#ffe6e6e6',GradientType=0);
    filter: progid:DXImageTransform.Microsoft.gradient(enabled=false);
    -webkit-box-shadow: inset 0 1px 0 rgba(255,255,255,0.2),0 1px 2px rgba(0,0,0,0.05);
    -moz-box-shadow: inset 0 1px 0 rgba(255,255,255,0.2),0 1px 2px rgba(0,0,0,0.05);
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.2),0 1px 2px rgba(0,0,0,0.05);
    margin-bottom: 10px;
}


.btn-warning {
    color: #fff;
    text-shadow: 0 -1px 0 rgba(0,0,0,0.25);
    background-color: #faa732;
    background-image: -moz-linear-gradient(top,#fbb450,#f89406);
    background-image: -webkit-gradient(linear,0 0,0 100%,from(#fbb450),to(#f89406));
    background-image: -webkit-linear-gradient(top,#fbb450,#f89406);
    background-image: -o-linear-gradient(top,#fbb450,#f89406);
    background-image: linear-gradient(to bottom,#fbb450,#f89406);
    background-repeat: repeat-x;
    border-color: #f89406 #f89406 #ad6704;
    border-color: rgba(0,0,0,0.1) rgba(0,0,0,0.1) rgba(0,0,0,0.25);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fffbb450',endColorstr='#fff89406',GradientType=0);
    filter: progid:DXImageTransform.Microsoft.gradient(enabled=false);
}
<div class='btn btn-warning like' style='margin-top: -6px;'> Like <span class='like-count'>0</span></div>



People are also looking for solutions to the problem: unable to determine current zabbix database version: the table "dbversion" was not found.
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

251 javascript - how to pass a jquery variable to php and then display it
121 php - PHPUnit_Extensions_Selenium2TestCase return data when finish
672 php - JSON Android App, Retrieval of data issue
868 Why do I need array() in PHP, if PHP is a dynamic/weakly typed language?
59 jquery - Indefinate Pagination with PHP
713 crawling different languages of a page which is depending on sessions in php
18 PHP image script often gives error 500 internal server error
644 php - How to simulate an HTTP 500 error on my Wordpress site?
113 Keep the true ARRAY OBJECT between Javascript -to- PHP via Ajax?
543 php - How to access form element xyz[] in Javascript?

People are also looking for solutions to the problem: How do I preserve HTML from the XML in PHP?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.