收藏按钮动态更改名称


favorite button dynamic changing the name

在每个帖子页面上都有一个按钮来"喜欢",我如何检查帖子是否被喜欢并更改按钮名称,例如,"喜欢",如果不是,通过单击按钮就可以完成,但在后台的一切,我对javascript的了解很浅,但我知道我需要ajax。已经在php/mysql中设置了一个小脚本,当帖子已经被喜欢或更改为喜欢时,返回一个json { "favorite": 1},其自然状态为{ "favorite": 0}

因为您只能喜欢登录到站点的文章,所以用户id将由会话设置,并且文章的id将使用 post方法

发送。

,

<a href="site.com/post/like"> Favorite </a>

…对不起,我的英语不好

  1. 设置服务器上的状态,我假设状态在$favorite变量中<a id="1234" href="site.com/post/like"
    class="like <?PHP echo $favorite?"liked":""; ?>">Favorite</a>

  2. 点击

这样的

$(function() {
  $(".like").on("click",function() { 
    $.post("likeornot.php?id="+this.id,function(data) {
      $(this).toggleClass("liked",data.favorite==1);
      // you can set the html of the link here too
    });
});