最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Like Dislike Function AJAX - Stack Overflow

programmeradmin2浏览0评论

I have this jQuery AJAX Like Dislike Script

<script>
$(function(){ 
    var PostID = <?php echo $PostID;  ?>; 

    $('.like-btn').click(function(){
        $('.dislike-btn').removeClass('dislike-h');    
        $(this).addClass('like-h');
        $.ajax({
            type:"POST",
            url:"rate.php",
            data:'Action=LIKE&PostID='+PostID,
            success: function(){
            }
        });
    });
    $('.dislike-btn').click(function(){
        $('.like-btn').removeClass('like-h');
        $(this).addClass('dislike-h');
        $.ajax({
            type:"POST",
            url:"rate.php",
            data:'Action=DISLIKE&PostID='+PostID,
            success: function(){
            }
        });
    });
});
</script>

Now I want to transform this script to multi post Like Dislike System. How i can do this? HTML will look like this:

<a href="#" onclick="RateSystem(LIKE, 1)">Like</a>
<a href="#" onclick="RateSystem(DISLIKE, 1)">Dislike</a>

LIKE/DISLIKE will be action, 1 will be ID of post to like/dislike. Thanks

I have this jQuery AJAX Like Dislike Script

<script>
$(function(){ 
    var PostID = <?php echo $PostID;  ?>; 

    $('.like-btn').click(function(){
        $('.dislike-btn').removeClass('dislike-h');    
        $(this).addClass('like-h');
        $.ajax({
            type:"POST",
            url:"rate.php",
            data:'Action=LIKE&PostID='+PostID,
            success: function(){
            }
        });
    });
    $('.dislike-btn').click(function(){
        $('.like-btn').removeClass('like-h');
        $(this).addClass('dislike-h');
        $.ajax({
            type:"POST",
            url:"rate.php",
            data:'Action=DISLIKE&PostID='+PostID,
            success: function(){
            }
        });
    });
});
</script>

Now I want to transform this script to multi post Like Dislike System. How i can do this? HTML will look like this:

<a href="#" onclick="RateSystem(LIKE, 1)">Like</a>
<a href="#" onclick="RateSystem(DISLIKE, 1)">Dislike</a>

LIKE/DISLIKE will be action, 1 will be ID of post to like/dislike. Thanks

Share Improve this question asked Jun 22, 2015 at 12:15 Miljan IlicMiljan Ilic 3353 silver badges13 bronze badges 2
  • This looks ok. What do you mean with "multi post"? – pid Commented Jun 22, 2015 at 12:18
  • This script is for one post per page, but I want to make it for more posts on one page . For thisI need function but I'm totaly noob with JavaScript, jQery... – Miljan Ilic Commented Jun 22, 2015 at 12:21
Add a ment  | 

2 Answers 2

Reset to default 3

you can make something like this. Each post it is each div with postId and two controls inside (like and dislike buttons). When you click like - function will get post id and send with post to server. You must to check ajax part of function.

$(function () {
  $('.like').click(function () { likeFunction(this); });
  $('.dislike').click(function () { dislikeFunction(this);});
});


function likeFunction(caller) {
  var postId = caller.parentElement.getAttribute('postid');
  $.ajax({
      type: "POST",
      url: "rate.php",
      data: 'Action=LIKE&PostID=' + postId,
      success: function () {}
  });
}
function dislikeFunction(caller) {
  var postId = caller.parentElement.getAttribute('postid');
  $.ajax({
      type: "POST",
      url: "rate.php",
      data: 'Action=DISLIKE&PostID=' + postId,
      success: function () {}
  });
}
<div class="post" postid="10">
    <input type="button" class='like' value="LikeButton" /> </input>
    <input type="button" class='dislike' value="DislikeButton"> </input>
</div>

If you have some questions or need more help you can ask me :)

If you want more than one post per page you can do this:

function send(action, id)
{
    var opposite;

    opposite = action === 'like' ? 'dislike' : 'like';

    $('#' + opposite + '-' + id).removeClass(opposite + '-h');
    $('#' + action + '-' + id).addClass(action + '-h');

    $.ajax({
        type:"POST",
        url:"rate.php",
        data:'Action=' + action + '&PostID=' + id,
        success: function(){
        }
    });
}

Now you only need to attach the handlers properly ($PostID must be different for every post in a loop):

<a href="#"
    id="like-<?php echo $PostID; ?>"
    onclick="send('like', <?php echo $PostID; ?>)">Like</a>
<a href="#"
    id="disloke-"<?php echo $PostID; ?>"
    onclick="send('dislike', <?php echo $PostID; ?>)">Dislike</a>

That's just a layout of the code, there may be defects. It's pretty different from where we started, so only you can actually test it and see where it need refinement.

发布评论

评论列表(0)

  1. 暂无评论