我不想要不同的系统,我的代码既可以兼顾也可以。任何人帮我解决这个问题我尝试用JavaScript编辑,但我是JavaScript初学者。如果有人帮助我,我将不胜感激。我试着用if else来解决这个问题但是无法调试..
<?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;"; } ?>" /> (<span id="likes_<?php echo $profile_id; ?>"><?php echo $total_likes; ?></span>)
<?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;"; } ?>" /> (<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));
}
});
});
});
答案 0 :(得分:1)
这
// like and unlike click
$(".like , .unlike").click(function(){
到
// like and unlike click
$(".like").click(function(){
从unlike
事件
click
课程