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 6752123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:00:28+00:00 2026-05-26T13:00:28+00:00

I have a table where a user clicks on image to block/unblock user. which

  • 0

I have a table where a user clicks on image to block/unblock user. which is something like this.

enter image description here

the html for the table (including the column for block/unblock) is retrieved via AJAX as i am using the jquery data table plugin.

here is the retrieved html for the column block/unblock

<td>
    <a class="toggle" href="#toggle">
        <img src="app/css/images/tick.png" alt="Yes" data-id="4" data-block="0">
    </a>
</td>

the data-table block represents the state of block/unblock, if the value is 0 then it shows an image called tick.png (enabled) else tock.png(disabled).

i am using the following jQuery to capture the click and send data to server side to get and make changes.

$('.toggle').live('click', function() {
    var id = $(this).find('img').data('id');
    var block =  $(this).find('img').data('block');
    $.ajax({
        type: 'POST',
        url:  "app/ajax/User/Block.php",
        data: 'id='+id+'&block='+block,
        success: function(data) {
            var image = (data == 1) ? 'app/css/images/tock.png' : 'app/css/images/tick.png';
            $('.toggle img[data-id="'+id+'"]').attr('data-block', data);
            $('.toggle img[data-id="'+id+'"]').attr('src', image);
        }
    });
});

and here is the PHP code i am using.

if(isset($_POST['id']) && isset($_POST['block'])) {
    $userId = $_POST['id'];
    //$_POST['blocks'] holds the current value of block/unblock.
    //toggle the block/unblock state.
    $block = $_POST['block'] == 1 ? 0 : 1;
    User::blockUser(array($userId), $block);
    //Respond back toggled block/unblock value.
    echo $block;
}

everything works fine if i click the image toggles the state so as the data-block value get updated, the problem is i can click it only once, if i click the same image again it doesn’t do anything. i checked the values in console, the values does not get updated too after the first click. what is wrong with the code?

Update :

Here is the HTML in the first stage before i.e before clicking.

<a class="toggle" href="#toggle">
    <img src="app/css/images/tick.png" alt="Yes" data-id="4" data-block="0">
</a>

here is the HTML after clicking the same row.

<a class="toggle row_selected" href="#toggle">
    <img src="app/css/images/tock.png" alt="Yes" data-id="4" data-block="1">
</a>
//Here the extra class row_selected is added by default by another plugin, do you think this is creating the problem?

and here it is again after first click.

<a class="toggle" href="#toggle">
    <img src="app/css/images/tock.png" alt="Yes" data-id="4" data-block="1">
</a>
  • 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-05-26T13:00:28+00:00Added an answer on May 26, 2026 at 1:00 pm

    jQuery has its own data implementation:

    You should use .data('block', data); instead of .attr('data-block', data);
    as you read it with $(this).find('img').data('block')

    Also you shouldn’t use jQuery for class detection like $('.toggle') if you know the tag.
    In your example you could easily improve the performance by using $('a.toggle')

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

Sidebar

Related Questions

I have a table User which has an identity column UserID , now what
I have a table cell, and when the user clicks on it, I replace
I have a table like so: Table eventlog user | user_group | event_date |
I have a user table in my mysql database that has a password column.
I have a method used to save an image when the user clicks Save.
I have a table (user) that contains user information. I have another table (userview)
I have a table with user items. Each user may have several types of
I'm just learning ruby on rails and I have a table of user roles
We have a user table, every user has an unique email and username. We
I have a User table that has all of their avatars saved in an

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.