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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:30:12+00:00 2026-05-20T15:30:12+00:00

I’m building a db driven jokes website where the users can vote the jokes

  • 0

I’m building a db driven jokes website where the users can vote the jokes up. When someone votes, the joke’s “score” column in the mysql db is incremented. On success, I’d like the joke’s score updated on the page. I have 2 questions – how do I select the associated score that is displayed and how do I query the database to get the updated value. Here is the jquery to run ajax:

 <script type="text/javascript">
 $(function() {
   $(".votebutton").click(function() {
      var id = $(this).attr('id');
      var ip = $('.ipz').attr('id');
      var dataString = 'id=' + id + '&ip=' + ip;
      $.ajax({
        type: "POST",
        url: "bin/vote.php",
        data: dataString,
        success: function() {
          // find the associated joke's score and query db for new score, or increment it, either is fine.
        }
      });
      return false;
   });
 });
 </script>

And here’s how the joke’s constructed so you can see what the score’s div id is:

 <div class="jokecontainer" id="joke_<?php echo $row['id']; ?>">
      <div class="score" style="float: left; width: 110px; height: 85px;">
           <p class="scorenum" id="score_<?php echo $row['id']; ?>"><?php echo $row['score']; ?></p>
           <p class="scorevote"><a class="votebutton" id="<?php echo $row['id']; ?>" href="#">VOTE UP</a></p>
      </div>
      <div class="joke">
           <span class="joketext"><?php echo $row['joke']; ?></span>
      </div>
      <div class="jokeinfo" style="float: left; width: 200px; height: 85px; margin-top: 5px;">
           <span class="jokeinfo1">Date: </span><span class="jokeinfo2">date</span><br />
           <span class="jokeinfo1">Author: </span><span class="jokeinfo2"><?php echo $row['user']; ?></span><br />
           <span class="jokeinfo1">Category: </span><span class="jokeinfo2"><?php echo $row['category']; ?></span><br />
      </div>
 </div>

The score’s id is this: id=score_ then the id. For example, if a votebutton with id of 123 is clicked, the score’s id would be “score_123”.

I think what I’m trying to do is construct an id selector that has: “score_” and the existing var id:

 var scoreid = "#score_" + id
 $(scoreid).html();

Then the second part – I’m thinking I store the score as a jquery variable and then increment it…. no idea how to start that.

THanks

  • 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-20T15:30:13+00:00Added an answer on May 20, 2026 at 3:30 pm

    I’d modify the PHP to return the new score value as a response to an upvote, as a JSON object. Assuming your response is something like (you might want to make this more complex by having it be a representation of your joke object – so you could have more keys and values such as the id as well )

    {"newScore" : 12}
    
    //or if you want, something more complex like - note the script below assumes the first JSON string
    {"joke":{"id":123456, "score":12, "author":"someone", "date":"2012-12-12T00:00:00"}}
    

    You can then parse the JSON and insert the new score just like you mentioned you were thinking of trying:

    <script type="text/javascript">
     $(function() {
       $(".votebutton").click(function() {
          var id = $(this).attr('id');
          var ip = $('.ipz').attr('id');
          var dataString = 'id=' + id + '&ip=' + ip;
          $.ajax({
            type: "POST",
            url: "bin/vote.php",
            data: dataString,
            dataType: 'json', //specify that the response content-type is expected to be JSON
            success: function(data) {
              // expect server to return the updated data score
                if(data.newScore) {
                   $("#score_" + id).html(data.newScore);
               }
               else {
                //handle error condition
               }
            }
          });
          return false;
       });
     });
     </script>
    

    This might be of some use if multiple users are voting up at the same time – the second voter will see the correct final value which will be the sum of his vote and the previous voter’s vote (i.e. he will see a +2 result instead of a +1). However, the first voter still sees a stale value and the second voter might get confused by the increase by 2 instead of 1 so this is something you need to think about more if you’d like to get something more real-time, although I think it’s probably not worth it – having a heartbeat that checks for updates of certain content on the page (kinda like here on SO) might be a good compromise.

    If you’d rather just increase the value of the current vote on the page by one on success without having the server return anything, you could do something like this (and note that you’d want to optimize this snippet and add some error checking):

    success: function() {
        var newScore = parseInt($("#score_" + id).html()) + 1;
        $("#score_" + id).html(newScore);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.