【问题标题】:how to like/unlike with button using toggleclass如何使用切换类来喜欢/不喜欢按钮
【发布时间】:2017-08-25 05:50:29
【问题描述】:

这是我喜欢的按钮...

HTML 代码

<div class="btn"> 
  <div class="boxcoracao">
    <span class="coracao" id = "<?php echo $postid?>" name = "like">br>&emsp;&emsp;&emsp;Love</span>
  </div>
</div>&emsp;&emsp;&emsp;

HTML 中的 Jquery

<script>
 $(".btn ").click(function(){ 
   $('.boxcoracao .coracao', this).toggleClass("ativo"); 
}); 
</script>

当我单击并取消单击按钮时,按钮会发生变化,但我的问题是如何使用此功能将数据保存到我的数据库中。第一次单击按钮时的示例,它会喜欢..当我再次单击按钮时,它会有所不同。你能帮我如何查询这个吗?

喜欢表格

postid  |  postmember |  likeid   
   50          12           1  

postid 的名称本身就是帖子的 id.. postmember 是发布示例用户 12 发布的用户的 id,id 为 50.. likeid 是喜欢其他用户示例用户 1 的帖子喜欢用户 12 的帖子的用户..

【问题讨论】:

  • if ($(this).hasClass("ativo")) { ...
  • 如果您的likeid 将在一个列中存储许多人(如用户ID 数组),那么您需要从likeid 数组中删除该人的ID。如果我理解正确的话。例如,如果有 5 个人喜欢该帖子,则类似于 postid = 50 |邮递员 = 12 | likeid = 1, 22, 1001, 21, 44。如果我想点击不像 then 并且如果我以会员 22 身份登录,那么 likeid 将 = 1, 1001, 21, 44。正确吗?
  • 如果很多用户喜欢数据库中的帖子就会发生这种情况例如如果有5个人喜欢帖子 postid = 50 |邮递员 = 12 | likeid = 1 postid = 50 |邮递员 = 12 | likeid = 22 postid = 50 |邮递员 = 12 | likeid = 1001 postid = 50 |邮递员 = 12 | likeid = 21 postid = 50 |邮递员 = 12 | likeid = 44 这是目前唯一的想法。存储数据..你的例子让我很困惑。就像我如何在 1 列中存储一组数据。对不起,我在这个领域真的很新,所以这是我现在唯一的方法。。
  • 非常感谢,这些是我真正需要的,但是当我刷新页面时,我遇到了另一个问题,我以前喜欢的帖子不被喜欢...
  • 当你第一次加载页面时,你需要查询你的数据库,然后做一个简单的计数来看看有多少喜欢的帖子ID。有很多方法可以做到这一点,但简单搜索 php count 会给你一些详细的答案。祝你好运。

标签: jquery html mysql ajax


【解决方案1】:

使用 ajax 是一种选择。

$("#postWrapper").on("click", ".likeToggle", function(){
	
	// grabe the variables you need

	var postid = $("#postid").attr("data-postid"); //postid
	var postmember = $("#postmember").attr("data-postmember"); // postmember
	var likeid = $("#likeid").attr("data-likeid"); // likeid

  $(this).toggleClass("likeColor");
  
  if ($(this).hasClass("likeColor")){
    
    console.log("LIKE");    
    $(this).text("dislike"); // update the text to show what the next click would be
    togglePost("like", postid, postmember,likeid); // run function
    
  } else {
  
    console.log("DISLIKE");
    $(this).text("like"); // update the text to show what the next click would be
    togglePost("dislike", postid, postmember,likeid); // run function
    
  }
 // send ajax to process.php

 
 function togglePost(action, postid, postmember, likeid){
	
  $.ajax({
        type: "post",
        url: "process.php",
        data: "action="+action+"&postid="+postid+"&postmember="+postmember+"&likeid="+likeid,
        success: function(data){
         
         alert("success");
          
        },
        error: function(e){
         alert("error");
        }
      });
      

 }

  });
.likeColor {
  background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<!-- index.php -->
<div id="postWrapper">
  <span id='postid' data-postid="50">POST ID: 50</span>
  <span id='postmember' data-postmember="12">CREATED BY: 12</span><br><br>
  <p>post contents are written here....</p><br>
  <br><hr><br>
  <div id='likeid' data-likeid="10">currently LOGGED IN as Member: 10</div><br>
 
  <button class="likeToggle">like</button>
</div>

<!-- process.php -->
<!--

  /*
  // you need to add 1 more row here and call it id and make it autoincrement INT or inserts wont work.
  
id = 1 | postid = 50 | postmember = 12 | likeid = 1
id = 2 | postid = 50 | postmember = 12 | likeid = 22
id = 3 | postid = 50 | postmember = 12 | likeid = 1001
id = 4 | postid = 50 | postmember = 12 | likeid = 21
id = 5 |postid = 50 | postmember = 12 | likeid = 44
*/

   $action = $_POST['action'];
   $likeid = $_POST['likeid'];
   $postmember= $_POST['postmember'];
   $postid = $_POST['postid']

 UpdateLikes($postid, $postmember, $likeid, $action);
  
 function UpdateLikes($postid, $postmember, $likeid, $action){
		
	if ($action == "dislike"){
		$query = mysql_query("DELETE FROM liketable WHERE 
			postid = '$postid' &&
			likeid = '$likeid'
			");
	} else {
		
		// before inserting you might want to check if they alredy liked or not before adding their count again.
		$query = mysql_query("INSERT INTO liketable
			( postid,  postmember, likeid ) VALUES 
			  ('$postid','$postmember','$likeid')");
	}
		
  }

  -->

【讨论】:

  • 你能告诉我process.php上的条件/if语句吗?我认为这是我最需要的......我只是不知道该怎么做......
  • 我更新并测试了它。我在我的数据库中创建了一个名为 likeFlag 的列,它是一个 tinyint。常规的 int 也可以,我相信 bool 类型也可以。 ** 我也没有向你展示 mysqli 的做法。我将把它留给你。
  • 如果我再次点击按钮与帖子不同怎么办??如果我错了,请纠正我..if ($action == "toggleLike") { UpdateLikeFlag($id); }else{ unlikeflag($id); } UpdateLikeFlag($id){ $query = mysql_query("UPDATE yourTable SET likeFlag = 1 - likeFlag WHERE id = '$id'"); } unlikeflag($id){ $query = mysql_query("UPDATE yourTable SET likeFlag = 1 - likeFlag WHERE id = '$id'"); }
  • 不,你不需要这样做。在查询中它将翻转数字。检查上面的 process.php 以获取详细信息。我在那里添加了解释。这实际上是一个切换。不需要 if 语句。
  • ohhhh 我认为这里有误导性..让我更新我的帖子对不起我没有包括我的表格字段..我理解你的代码,但我认为它不适合我的表格字段。跨度>
【解决方案2】:

试试这个:

$(".btn ").click(function(){ 
   $('.boxcoracao .coracao', this).toggleClass("ativo");
   // make an ajax call here to send the data to the server and save it in database
});

如果条件是这样的:

var selection = '';
if( $(this).val() == 'like' )
{
    selection = 'liked';
}
else
{
    selection = 'like';
}

ajax() 调用中传递此变量selection

并在页面重新加载时检查数据库中的状态以分别显示喜欢/不喜欢的图标。

【讨论】:

  • 你能提供一个 if 语句的例子吗?因为当用户不像按钮时,我真的不知道该怎么做
【解决方案3】:

您需要在 click 函数中包含一个 if 语句。

$(".btn ").click(function(){ 
   $('.boxcoracao .coracao', this).toggleClass("ativo"); 

  if( $(this).hasClass("ativo") ) {
    // the query to exicute if the class has been added
  }
  else {
    // the query to exicute if the class has been removed
  }
}); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-20
    • 2016-04-04
    • 2014-09-21
    • 2019-07-03
    • 2017-09-06
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    相关资源
    最近更新 更多