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

  • Home
  • SEARCH
  • 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 6812037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:24:03+00:00 2026-05-26T20:24:03+00:00

My page displays posts stored in my database and pairs a like/dislike button to

  • 0

My page displays posts stored in my database and pairs a like/dislike button to each post.
I am trying to make it so that when a user clicks like or dislike, the value is sent to my database for later recollection.

I have a complex loop that I hope to explain:
On page load, an ajax call is made to an external php file to fetch data from my database. The result is an object array which holds “postID”:value. Inside a for loop, I am storing the value inside var postID for easy access. I’m obviously going about this wrong because the code does execute, but only sends the value and postID of the last occurrence in the loop no matter which pair of buttons I press: If I have 11 posts in my database and I press “like” on post 3 it will store the value ‘like’ but for post 11. If I press dislike on post 7 it will store the value ‘dislike’ but for post 11.

So the question is, how do I fix this so when I press like or dislike for post x, it will store the value for that same post and not the last post in my database?

The array looks like this:

{"s":0,"d":[{"postID":"1"},{"postID":"2"},{""pos‌​tID":"3"},{""pos‌​tID":"4"},{""pos‌​tID":"5"},{""pos‌​tID":"6"},{""pos‌​tID":"7"},{""pos‌​tID":"8"},{""pos‌​tID":"9"},{""pos‌​tID":"10"},{""pos‌​tID":"11"}]}

The post divs are the following format:

<div id="post_#"> //# representing the postID in the database
    <div id="like_#"></div> //# repesenting the postID in the database
    <div id="dislike_#"></div> //# representing the postID in the database
</div>

My jQuery (includes only the like button):

$(document).ready(function() {
    $.ajax({
        url: 'postID.php', //fetch the above array
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (post) {
                    if (post.s == 0) {
                    //Loop through returned data array using: post.d
                        for (var key in post.d) {
                            var obj = post.d[key];
                            for (var prop in obj) {
                                var postID = obj[prop];

                                //When like button is clicked do this
                                $('#like_' + postID).click(function() {

                                    // Declare variables
                                    var value = '1';

                                    // Send values to database
                                    $.ajax({
                                        url: 'check.php', //check.php receives the values sent to it and stores them in the database
                                        type: 'POST',
                                        data: 'postID=' + postID + '&value=' + value,
                                        success: function(result) {
                                            $('#Message_'+ contestID).html('').html(result).prependTo('#post_' + postID);
                                        }
                                    });
                                 });
                             }
                         }
                     }
                  }
     });
});

So again, my problem is that the second ajax loop is only sending the last instance of var postID instead of sending the postID of the post that was actually clicked.

Any ideas?

  • 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-26T20:24:04+00:00Added an answer on May 26, 2026 at 8:24 pm

    *updated to use .on() instead of .live() as its deprecated since 1.7
    try this instead:

    html

    <div id="post_#" class="post"> //# representing the postID in the database
        <div id="like_#" class="like">test</div> //# repesenting the postID in the database
        <div id="dislike_#" class="dislike"></div> //# representing the postID in the database
    </div>
    

    jquery

    $(document).ready(function() {
    
    //When like button is clicked do this
    $('.like').on('click', function() {
    
        var postID = $(this).attr('id').replace('like_', '');
    
        // Declare variables
        var value = '1';
    
        // Send values to database
        $.ajax({
            url: 'check.php',
            //check.php receives the values sent to it and stores them in the database
            type: 'POST',
            data: 'postID=' + postID + '&value=' + value,
            success: function(result) {
                $('#Message_' + contestID).html('').html(result).prependTo(this);
            }
        });
    });
    
    });
    

    Here is the link to a sample http://jsfiddle.net/TXwZk/

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

Sidebar

Related Questions

My page shows posts stored in my Database through a loop. Each post is
I have a page that displays posts and I want to get the name
I have a page that displays a table in two different modes. In each
I have a page that displays search results and has a DOM like the
I have a page that displays a list of records. The user can select
I have a page that displays a list with a of elements with a
i have a page that displays large datasets into html tables. how can i
I have a page that displays messages and I want it to work just
I have a php page that pulls data from a mysql database based on
I have a website that is fairly database intensive, so I'm trying to cut

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.