SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    0 Post(s)
    0 Thread(s)

    [jQuery] Live increment update

    Hello I am working on a voting script.

    So far I have this placed in a PHP while loop.

    jQuery("#up a").click(function () {
        type: "POST",
        url: "vote.php?up=<? echo  $row['post_id']; ?>",
    $row['vote_count'] // Displays the number of votes
    When a user clicks the button it processes the vote.php page and adds a vote to the database. How can I use jQuery to simply increment the $row['vote_count'] result by +1 without refreshing the page.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Melbourne, AU
    17 Post(s)
    1 Thread(s)
    The most simple way would be to declare a var outside of the click scope that you can increment with each click:

    Code JavaScript:
    var totalVotes = <? echo $row['vote_count']; ?>;
    $("#up a").click(function () {
            type: "POST",
            url: "vote.php?up=<? echo  $row['post_id']; ?>",
            success: function() {

    On a side note i found the following two things that you should watch out for next time when coding in JavaScript:

    1. You mixed your jQuery declarations between $ and jQuery, you should use either one or the other but never both at the same time as it's bad practice in my opinion
    2. You had an extra comma in your object you passed into your $.ajax() method, this will cause an error in IE so just something to watch out for


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts