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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:36:15+00:00 2026-06-16T19:36:15+00:00

My issue is am using jQuery to auto refresh the div but am using

  • 0

My issue is am using jQuery to auto refresh the div but am using the following script to keep the record track, so if the user clicks on Load More I load the posts, but the moment jQuery refreshes the div, it resets back to the default, any idea how do I preserve the number of loaded record

This is what I tried

if(isset($_POST['load_more'])) {
    $_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
    $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
} else {
    $counter = 10;
    $_SESSION['total_counter'] = $counter;
    $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
}

What happens is on refresh it uses the $counter value and forgets the load more session value

Detailed Code

<?php
    require_once 'connection.php';
    require_once 'functions.php';
    if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false) {
        header('Location: login.php');
        exit;
    }
    date_default_timezone_set("Asia/Calcutta");
?>
<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                jQuery(".toggle_container").show();
                jQuery("div.question_trigger").click(function(){
                jQuery(this).toggleClass("active").next().toggle("slow");
                });
            });
        </script>
        <script type="text/javascript">
            var auto_refresh = setInterval(function() {
                  $('.messages').load('chat.php .messages', function(){
                       $(this).fadeIn("fast")
                  });
            }, 2000);
        </script>
    </head>
    <body>
        <?php
            require_once 'header.php';
            if($_SESSION['user_id'] == 1) {
                $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_first = 'yes' WHERE checked_first = ''");
            } elseif ($_SESSION['user_id'] == 2) {
                $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET checked_second = 'yes' WHERE checked_second = ''");
            } elseif ($_SESSION['user_id'] == 3) {
                $clear_notifications = mysqli_query($connection, "UPDATE tbl_chat_notifications SET  checked_third = 'yes' WHERE checked_third = ''");
            }   
        ?>
        <div class="container">
            <form method="POST">
                <textarea name="messaging"></textarea>
                <input type="submit" value="Post" name="message_input" class="gen_button" />
            </form>
            <img src="smileys/smile.gif" /><a target="_blank" href="smileys_info.php"> Smileys Info</a>
            <br /><br />
            <?php
            if(isset($_POST['message_input'])) {
                $throw_error = array();
                if(isset($_POST['messaging']) && $_POST['messaging'] == '') {
                    $throw_error['msg_blank'] = "Message Cannot Be Blank";
                }

                if(empty($throw_error)) {
                    $message = nl2br(htmlspecialchars(mysqli_real_escape_string($connection, $_POST['messaging'])));
                    $date_time = date("Y-m-d H:i:s");
                    if(!mysqli_query($connection, "INSERT INTO tbl_chat (msg_from, message, record_date) VALUES('".$_SESSION['user_id']."', '$message', '$date_time')")) {
                        echo mysqli_error($connection);
                    }

                    if($_SESSION['user_id'] == 1) {
                        if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_first) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                            echo mysqli_error($connection);
                        } 
                    } elseif ($_SESSION['user_id'] == 2) {
                        if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_second) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                            echo mysqli_error($connection);
                        }
                    } elseif ($_SESSION['user_id'] == 3) {
                        if (!mysqli_query($connection, "INSERT INTO tbl_chat_notifications (notification_of, checked_third) VALUES('".$_SESSION['user_id']."', 'yes')")) {
                            echo mysqli_error($connection);
                        }
                    }
                    else {
                        header('Location: chat.php');
                        exit;
                    }
                }

                if(!empty($throw_error)) {
                    if(isset($throw_error['msg_blank'])) {
                        echo "<span class=\"red\">".$throw_error['msg_blank']."</span>";
                    }
                }
            }

            if(isset($_POST['load_more'])) {
                $_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
                $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);

            } else {
                $counter = 10;
                $_SESSION['total_counter'] = $counter;
                $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
            }
            ?>
            <div class="messages">
            <?php
                while($throw_messages = mysqli_fetch_array($fetch_messages)) {
            ?>
            <div class="message_container">
                <div class="display_pic">
                    <img src="<?php echo $throw_messages['user_pic']; ?>" />
                </div>
                <div class="user_name">
                    <?php
                        echo $throw_messages['real_name'];
                    ?>
                </div>
                <div class="message"><?php smilies($throw_messages['message']); ?></div>
                <div class="msg_date_time"><?php echo explode_date_time($throw_messages['record_date']); ?></div>
            </div>
            <?php } ?>
            </div>
            <?php
                if(mysqli_num_rows($fetch_messages) > 9) {
            ?>
            <form method="POST">
                <input type="submit" value="Load More" name="load_more" />
            </form>
            <?php
                }
            ?>
        </div>
    </body>
</html>
  • 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-16T19:36:16+00:00Added an answer on June 16, 2026 at 7:36 pm

    Ok so I got it, It was resetting the counter on page load because I was using tt was only adding up the value if the load_more form was submitted but as it was refreshed by jQuery it was resetting the counter value, this is the correct condition

    if(!isset($_SESSION['total_counter'])) {
        $_SESSION['total_counter'] = 10;
    }
    
    if(isset($_POST['load_more'])) {
        $_SESSION['total_counter'] = $_SESSION['total_counter'] + 10;
        $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
    } else {
        $fetch_messages = mysqli_query($connection, "SELECT tbl_chat.record_date, msg_from, message, user_pic, tbl_chat.id, tbl_chat.record_date, real_name FROM tbl_chat JOIN tbl_users ON msg_from = tbl_users.id ORDER BY id DESC LIMIT ".$_SESSION['total_counter']);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I faced the following issue while I submitting my form using jQuery FORM and
I'm running into a character encoding issue when I load a dropdown using jQuery
I am currently able to refresh a div on my website using jquery with
For some days i´m battling with this issue: i´m using Jquery to extract data
I am developing an app using jquery mobile and last issue to be solved
I am using jquery modal popup in my application with MVC3 Razor view. Issue
I am using jquery fullcalendar which works great. The one issue I have is
I'm having an issue using the qTip plugin with jQuery. I've created a js
Here's my issue - I need to dynamically download several scripts using jQuery.getScript() and
I'm having an issue verifying if a checkbox is checked using jquery on Internet

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.