Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8859441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:03:49+00:00 2026-06-14T15:03:49+00:00

I am building up a Like/Unlike system, I have a button what has a

  • 0

I am building up a Like/Unlike system, I have a button what has a class like, if i click it data is inserted in the database, and the class is changed to unlike.

And unlike suppose to pull another ajax call what removes the actual like but its not working, when the class changed it preforms what like class suppose to do, and it only works if i refresh the page.

php code

<div class="btn-group pull-right">
<?php
    $like = '<button class="btn btn-primary btn-small like" data-like="'.$user->id.'">
        <i class="icon-thumbs-up icon-white"></i> Like
    </button>';  
    foreach ($user->likes as $likes) {
        if($likes['liked_by'] == Session::get('sentry_user')) {
          $like = '<button class="btn btn-primary btn-small unlike" data-like="'.$user->id.'">
            <i class="icon-thumbs-down icon-white"></i> Unlike
            </button>'; 
            break 1;
         }
    }
    echo $like; 
 ?>
</div>

jquery

$('button.like').bind('click', function(){
    var likeId = $(this).data('like');
    $.ajax({
        url: siteUrl + 'profile/like',
        type: "post",
        data: {user_id: likeId},
        dataType: "json",
        context: this,
        beforeSend: function()
        {
            $(this).addClass('disabled');
        },
        success: function(data)
        {
            if(data.status == "like") {
                $(this).removeClass('like')
                .addClass('unlike')
                .append()
                .html('<i class="icon-thumbs-down icon-white"></i> Unlike');
            }
        },
        complete: function()
        {
            $(this).removeClass('disabled');
        }
    });

});

$('button.unlike').bind('click', function(){
    var likeId = $(this).data('like');
    alert('you are about to unlike');


})

just made the unlike alert for an example to test it befoe I make the ajax call with it.

So cloud someone give me a hint?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:03:50+00:00Added an answer on June 14, 2026 at 3:03 pm

    i’ve done something similar on my app, i used this logic:

    html:

    <a class="like" onclick="like($(this))"></a>
    

    js:

        function like(_element){
    
        if($(_element).hasClass('unlike')){
    
    $.ajax(); //do unlike
        $(_element).removeClass('unlike'); // this goes inside the success:function(){} of the ajax call
    
    }else{
    
         $.ajax(); //do like    
         $(_element).addClass('unlike'); // this goes inside the success:function(){} of the ajax call
    
    }
    
        }
    

    also you can refactoring this example for using just 1 ajax call, you will have less code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i am building a 'like' button but i have a small problem after
I'd like to play around with building a recommendations system, and by that I
I'm building an app with iPod-like controls (play, pause, etc). The app has tableView
I am building a waterfall view just like pinterest. Now I have to explicitly
I have a 'hash tree' like this: dat = { 'building' => {'street' =>
I have an array I am building like this: foreach($items as $item) { $this->_array[(int)$item->getPosition()]
I have no idea about any building tool like Ant, Maven, or Gradle. This
I'm building something like in the question How to collect data from different .a
I have an array I'm building like this: Post.where(:user_id => current_user.id, :status_id => 2).select(:id).inspect.to_a
I am building a simple like system for my website. The problem is that,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.