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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:16:13+00:00 2026-06-08T18:16:13+00:00

I need a suggestion with live update messages, here is my code: <script> function

  • 0

I need a suggestion with live update messages, here is my code:

<script>
    function fetch_messages(){
        var user_id = "1" // example id

        $.ajax({
            url: "do_fetch.php",
            type: "POST",
            data: { user_id : user_id },
            dataType: "json",
            success: function(data) {
                for (var i = 0; i < data.length; i++) {
                    $("#messages_list").append('<div id="'+data.id+'">'+data.message+'</div>');
                }
                setTimeout("fetch_messages()", 3000);
            }
        });
    }
    $(function(){
        fetch_messages();
    });
</script>

and there is div messages:

<div id="messages">
</div>

php code:

<?php
    include('connection.php')

    $user_id = $_POST['user_id'];
    $my_id = $_SESSION['user_id'];

    $sql = 'SELECT * FROM messages WHERE user_id = '.mysql_real_escape_string($user_id).' AND my_id = '.mysql_real_escape_string($my_id);
    $result = mysql_query($sql, $connection);

    echo json_encode($result);
?>

This code works, but I don’t want to read all messages and show them, I need to load only new messages, so how should I do that? Do I need to put a hidden field with timestamp and to check with ajax for time later than that?

  • 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-08T18:16:14+00:00Added an answer on June 8, 2026 at 6:16 pm

    First you load with ajax just 20 messages in a ul

    <ul>
      <li>message 1</li>
      <li>message 2</li>
      <li>message 3</li>
      <li>message 4</li>
      <li>message 5</li>
      <li id="{{message_date}}">message 6</li>
    </ul>
    

    Then get the last message id to get the date.

    $('li').last().attr('id')
    

    you can have a click button to load more.

    var message_date = $('li').last().attr('id');
    $.ajax({
        url: "do_fetch.php",
        type: "POST",
        data: { date : message_date },
        dataType: "json",
        success: function(data) {
            for (var i = 0; i < data.length; i++) {
                $("ul").append('<liid="'+ data.date+'">'+data.message+'</li>');
            }
        }
    });
    

    In the php file you select the messages with date > message_date with limit 30.

    You can pass the user id too.

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

Sidebar

Related Questions

Locally I need to code against Java 1.4. I tried the suggestion in this
with following code qTip works for me and generates tooltips: $('a.ppname[rel]').live('mouseover', function() { $(this).qtip(
I have a live function that triggers the 'openwindow' function and I need to
I need suggestion about YAMI library . I have a system which receives Json
I know my question isn't about programming problem, I just need a suggestion to
I need a excel-like grid control in MFC, do anyone have good suggestion to
I need some asp.net pagination code samples. I would like suggestions on open source
I need to re-index all of my domain instances from a live legacy database
I need to keep the session live unless until the user clicks logout in
The second click function of this script doesn't work, I think it's simply a

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.