我有一个个人资料页面,在我的照片下面我有一个带计数器的按钮,我希望当我点击时,喜欢按钮的文字设置为"不像"但是,与所有用户相关的所有类似的个人资料页面都会留在那里。当我击中时不喜欢把我的回归。
我是乞丐,我搞砸了代码,所以请帮忙,我会感激不尽,谢谢!
这就是我到目前为止: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计数处理文件:
<?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文件:
<?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);
}
&#13;
<div class='btn btn-warning like' style='margin-top: -6px;'> Like <span class='like-count'>0</span></div>
&#13;