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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:46:22+00:00 2026-06-02T02:46:22+00:00

I’ve been trying for the last two days to get this to work with

  • 0

I’ve been trying for the last two days to get this to work with no luck. It executes one time, but fails to continue to update after the initial load. The function does not automatically update, nor does it relaunch after 30 seconds. The code seems fine to me but obviously there is some gap of knowledge I have regarding long-polling which is causing this not to work. If anyone has a moment I’d appreciate it if you could comb over this and tell me what it is I’m doing incorrectly. Any help is appreciated, thank you.

JavaScript/jQuery

function poll(pid){
    var dataString = 'pid=' + pid;
    $.ajax({type: 'GET', url: 'http://localhost:8888/mysite/execs/vote_count.php', data: dataString, async: true, cache: false, success: function(data){

 var post = $('#' +pid);
    var post_children = post.children();
    var upvotes = post_children.find('.upvotes');
    var downvotes = post_children.find('.downvotes');

downvotes.text("-" + data.downvotes);
upvotes.text("+" + data.upvotes);

    }, dataType: "json", complete: poll, timeout: 30000 });
};

$(".post").each(function(){
poll($(this).attr("id"));
});

PHP (vote_count.php)

<?php
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'database';
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare("SELECT * FROM vote WHERE post = :pid AND rating = 'votedown'");
    $pid = $_GET['pid'];
    $stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
    $stmt->execute();
    $result = $stmt->fetchAll();
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
    $stmt->execute();
    $downvotes = $stmt->rowCount();


    try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare("SELECT * FROM vote WHERE post = :pid AND rating = 'voteup'");
    $pid = $_GET['pid'];
    $stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
    $stmt->execute();
    $result = $stmt->fetchAll();
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
    $stmt->execute();
    $upvotes = $stmt->rowCount();
$arr = array("downvotes"=>$downvotes,"upvotes"=>$upvotes);
echo json_encode($arr);  
$dbh = null;
?>
  • 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-02T02:46:29+00:00Added an answer on June 2, 2026 at 2:46 am

    The major problem I think you’re having is in your assuming that complete: poll will use the passed-in parameter pid from the current call. Try this.

    function poll(pid){
        var dataString = 'pid=' + pid;
        $.ajax({type: 'GET', 
            url: 'http://localhost:8888/mysite/execs/vote_count.php', 
            data: dataString, 
            async: true, 
            cache: false, 
            success: function(data){
                var post = $('#' +pid);
                var post_children = post.children();
                var upvotes = post_children.find('.upvotes');
                var downvotes = post_children.find('.downvotes');
    
                downvotes.text("-" + data.downvotes);
                upvotes.text("+" + data.upvotes);
            }, 
            dataType: "json", 
            complete: function(xhr, status){
                setTimeout(function(){poll(pid);}, 30000);
            }, 
            timeout: 30000 
        });
    }
    
    $(".post").each(function(){
        poll($(this).attr("id"));
    });
    

    All that said, if you have many posts you’re checking updates for, you might consider bundling these calls up and calling once per page every 30 seconds (and return an array of post updates) instead of once per post per page every 30 seconds. (My extra two cents.)

    Edit: Added the timeout mentioned. Left it out the first time.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.