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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:44:49+00:00 2026-06-16T00:44:49+00:00

I’m working on a live discussion form. The page loads the threads from MySQL,

  • 0

I’m working on a live discussion form. The page loads the threads from MySQL, and refreshes via AJAX every 4 seconds. After the last comment on each thread, there’s a text input for comments (very similar to Facebook).

Everything works fine, except that when the user is writing in the input and the page refreshes, the message disappears. Security and privacy is the main goal on this project, that’s why I don’t want to use cookies. I have tried many solutions, and searched several hours for a solution but nothing seems to work.

  1. Is it a possible solution to $_post every time that page refreshes?
  2. In case of caching the entered values and retrieving them from $_session or local storage, can anyone suggest a more specific approach? i.e: where to put the listeners 🙂
  3. I’ve tried to make a function that prevents reloading the page if the value of the input is different to “”, but even this didn’t work for me.

Here is my refresh code:

<script type="text/javascript">
    var PHP = "msgboard.php";  

    function updateShouts(){
        $('#msgboard').load(PHP);       
    }

    window.setInterval( "updateShouts()", 4000 );
</script>

And here is the main PHP function:

while($row = mysql_fetch_array($resultados)) { 

    echo '<div class="post">
            <div class="user"><b>'.$row["user"].'</b></div>';
    echo '  <div class="txt">'.$row["msg1"].'</div>
          </div>';

        $sql2="SELECT * FROM table WHERE masterid = '".$row['id']."'ORDER BY id ASC";
        $resultados2 = mysql_query($sql2);
        while($row2 = mysql_fetch_array($resultados2)) { 
            echo '<div class="comment">
                    <div class="txt"><b>'.$row2['user'].'</b>';
            echo '  '.$row2['msg1'].'</div>
                  </div>';
        }

    echo '<div class="commentform">
            <form action="board.php" method="post">
            <input type="text" size="75" name="message" id="message1">
            <input type="hidden" name="masterid" value="'.$row['id'].'">
            <input type="submit" name="Submit" value="Enviar"></form>
        </div>' ;
}

Thanks in advance!

  • 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-16T00:44:50+00:00Added an answer on June 16, 2026 at 12:44 am

    As @aziz pointed out in the comments, if you just want to update the comments, you only need to update that part of your page.

    Step 1: Add a container to the comments so you can target the update to that container

    You will notice there is now a new div <div id="comments_contaner"> in the code which will contain all the comments.

    Also when you are outputting HTML, it is easier / more legile to just close the PHP tag and use <?= $variable ?> if you need to place a PHP variable in the HTML.

    msgboard.php:

    <?
    while($post = mysql_fetch_array($resultados))
    {
    ?>
        <div class="post">
            <div class="user"><b><?= $post["user"]?></b></div>
            <div class="txt"><?= $post["msg1"]?></div>
        </div>
        <div id="comments_contaner">
    <?
        // This code can be ommited as you can just call updateShouts() upon page load to fetch the comments
        $comments = mysql_query("SELECT * FROM table WHERE masterid = '{$post['id']}' ORDER BY id ASC");
        while($comment = mysql_fetch_array($comments))
        {
    ?>
            <div class="comment">
                <div class="txt"><b><?= $comment['user'] ?></b> <?= $comment['msg1'] ?></div>
            </div>
    <?
        }
    ?>
        </div>
        <div class="commentform">
            <form action="board.php" method="post">
                <input type="text" size="75" name="message" id="message1">
                <input type="hidden" name="masterid" value="<?= $post['id'] ?>">
                <input type="submit" name="Submit" value="Enviar">
            </form>
        </div>
    <?
    }
    ?>
    

    Step 2: Create a PHP function to update the comments

    This function will only output the comments, it will need the masterid as a parameter to know which comments to output.

    updateComments.php:

    <?
        $masterid = $_GET['masterid']
        $comments = mysql_query("SELECT * FROM table WHERE masterid = '{$masterid}' ORDER BY id ASC");
        while($comment = mysql_fetch_array($comments))
        {
    ?>
            <div class="comment">
                <div class="txt"><b><?= $comment['user'] ?></b> <?= $comment['msg1'] ?></div>
            </div>
    <?
        }
    ?>
    

    Step 3: Call the PHP update function in your script targeting the container div

    You will need to pass the $row['id'] as a parameter in the URL

    <script type="text/javascript">
        var PHP = "updateComments.php?masterid=<?= $post['id']?>";  
    
        function updateShouts(){
            $('#comments_contaner').load(PHP);       
        }
    
        window.setInterval( "updateShouts()", 4000 );
    </script>
    

    PD: I have not tested this code, it is just to show you the main idea.

    EDIT: Corrected variable names and added comment.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.