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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:51:37+00:00 2026-06-15T03:51:37+00:00

Background Info I’m fiddling around with some PHP and AJAX at the moment, to

  • 0

Background Info

I’m fiddling around with some PHP and AJAX at the moment, to try and get the code working for an auto refreshing div (every 10 seconds), that contains comments.

Here is javascript code I am using to refresh the div..

<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
        $.ajaxSetup({ cache: false }); 
            setInterval(function() {
                    $('#content_main').load('/feed_main.php');
        }, 5000);  
});
// ]]></script>

The code that will populate the div called “content_main”, which is in feed_main.php, essentially accesses the database and echo’s out the latest comments …

Question

Is it possible, to only load the div “content_main” if the data inside of it, hasn’t changed since the last time it was loaded?

My logic

Because I’m relatively new to javascript and AJAX I don’t quite know how to do this, but my logic is:

For the first time it is run..

  • load data from feed_main.php file
  • Create a unique value (perhaps a hash value? ) to identify say 3 unique comments

Every other time it is run…

  • load the data from feed_main.php file
  • create a NEW unique value
  • check this value with the previous one
  • if they’re the same, don’t refresh the div, just leave things as they are, but if they’re different then refresh..

The reason why I want to do this is because the comments usually have pictures attached, and it is quite annoying to see the image reload every time.

Any help with this would be greatly appreciated.

  • 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-15T03:51:39+00:00Added an answer on June 15, 2026 at 3:51 am

    I’ve faced similar problem not too long ago, i assume that you using mysql or something for your comments storage serverside ?

    I solved my problem by first adding timestamp integer column to my mysql table, then when i added a new row, i’d just simply use time() to save the current time.

    mysql row insert example:

    $query = "INSERT INTO comments (name, text, timestamp) VALUES ('". $name ."', '". $text ."',". time() .");";
    

    step two would be to json_encode the data you sending from serverside:

    $output = array();
    
    if ($html && $html !== '') {   // do we have any script output ?
      $output['payload'] = $html;  // your current script output would go in this variable
    }
    $output['time'] = time();      // so we know when did we last check for payload update
    
    $json = json_encode($output, ((int)JSON_NUMERIC_CHECK)); // jsonify the array
    echo $json;                    // send it to the client
    

    So, now instead of pure html, your serverside script returns something like this:

    {
      "payload":"<div class=\"name\">Derpin<\/div><div class=\"msg\">Foo Bar!<\/div>",
      "time":1354167493
    }
    

    You can grab the data in javascript simply enough:

    <script type="text/javascript"> // <![CDATA[
    
    var lastcheck;
    var content_main = $('#content_main');
    
    pollTimer = setInterval(function() {
      updateJson();
    }, 10000);
    
    function updateJson() {
      var request = '/feed_main.php?timestamp='+ (lastcheck ? lastcheck : 0);
    
      $.ajax({
        url: request,
        dataType: 'json',
        async: false,
        cache: false,
        success: function(result) {
          if (result.payload) {        // new data
            lastcheck = result.time;   // update stored timestamp
            content_main.html(result.payload + content_main.html()); // update html element
          } else {                     // no new data, update only timestamp
            lastcheck = result.time;
          }
        }
      });
    }
    
    // ]]> </script>
    

    that pretty much takes care of communication between server and client, now you just query your database something like this:

    $timestamp = 0;
    $where = '';
    
    if (isset($_GET['timestamp'])) {
      $timestamp = your_arg_sanitizer($_GET['timestamp']);
    }
    
    if ($timestamp) {
      $where = ' WHERE timestamp >= '.$timestamp;
    }
    
    $query = 'SELECT * FROM comments'. $where .' ORDER BY timestamp DESC;';
    

    The timestamps get passed back and forth, client always sending the timestamp returned by the server in previous query.

    Your server only sends comments that were submitted since you checked last time, and you can prepend them to the end of the html like i did. (warning: i have not added any kind of sanity control to that, your comments could get extremely long)

    Since you poll for new data every 10 seconds you might want to consider sending pure data across the ajax call to save substantial chunk bandwidth (json string with just timestamp in it, is only around 20 bytes).

    You can then use javascript to generate the html, it also has the advantage of offloading lot of the work from your server to the client :). You will also get much finer control over how many comments you want to display at once.


    I’ve made some fairly large assumptions, you will have to modify the code to suit your needs. If you use my code, and your cat|computer|house happens to explode, you get to keep all the pieces 🙂

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

Sidebar

Related Questions

Some background info: I am working on an opengl game for fun/experience, and in
Are the following assumptions valid for this code? I put some background info under
Some background info: I am currently writing a bootloader in protected mode while learning
Some background info: I'm trying to run a server program in python 2.5.1 (the
Can someone give me some info/background info on how I might go about writing
I would really like some advice here, to give some background info I am
BACKGROUND INFO: I need to update some data from the web, about every hour
Some Background info: My web application stores some XML in a Text column of
Here's some background info. I have three MySQL tables (all InnoDB). The first table
Hey everyone, some background info: In the config file for my website, I set

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.