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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:36:30+00:00 2026-05-16T11:36:30+00:00

When I run this code the page repeats its self. I also need it

  • 0

When I run this code the page repeats its self. I also need it to refresh, which it isn’t doing.

Like this:

Image

EDITED code below:

<?php include 'config.php' ?>
<script language="javascript">
function createRequestObject() {

   var req;

   if(window.XMLHttpRequest){
      // Firefox, Safari, Opera...
      req = new XMLHttpRequest();
   } else if(window.ActiveXObject) {
      // Internet Explorer 5+
      req = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      // There is an error creating the object,
      // just as an old browser is being used.
     alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
   }

   return req;

}

// Make the XMLHttpRequest object
var http = createRequestObject();

function sendRequest(page) {

   // Open PHP script for requests
   http.open('get', page);
   http.onreadystatechange = handleResponse;
   http.send(null);

}

function handleResponse() {

   if(http.readyState == 4 && http.status == 200){

      // Text returned FROM the PHP script
      var response = http.responseText;

      if(response) {
         // UPDATE ajaxTest content
         document.getElementById("msgstatus").innerHTML = response;
      }

   }

}


function repeatloop()
{
sendRequest('test.php'); // replace "inbox-status.php" with your php page's url
setTimeout("repeatloop()", 10000);
}

var replacementDiv = document.createElement("div");
replacementDiv.innerHTML = response;
document.getElementById("msgstatus").innerHTML = replacementDiv.firstChild.innerHTML;

window.onload=function() {
repeatloop();
}
</script>
<link rel="stylesheet" href="style.css" type="text/css" />
</head><body>

<?php // Collet Latest Posts

$query = "
    SELECT Users.UserID, Wall.Message, Users.Forename, Users.Surname 
    FROM Wall
    INNER JOIN Users ON Wall.UserID = Users.UserID
    ORDER BY Wall.MessageID DESC
    LIMIT 20;";
$result = mysql_query($query) or die('Invalid query: ' . mysql_error());

// Collet Post User
    ?>
    <div id ="container">
        <div id="insideleft">
            <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="profile.php">Edit Profile</a></li>
                <li><a href="wall.php">Community Wall</a></li>
                <li><a href="logout.php">Logout</a></li>
            </ul>
        </div>
        <div id="insideright">
            <h1>Community Wall</h1>
            <br />
            <div id="postcontainer">
                <form method="post" action="wall.php" name="wallpost" id="wallpost">
                    <input type="text" name="message" id="message" class="message" />
                    <input type="submit" name="messagesub" id="messagesub" value="Post Message" class="post"/><br /><br />
                 </fieldset>
                </form>
            </div>
            <span id="msgstatus">
            <?php while ($row = mysql_fetch_assoc($result)) { ?>
            <div id="messagecontainer">
            <img class="pic" src="dummy.gif">
            <p class="messageposter">
            <?php echo "<b>{$row['Forename']} {$row['Surname']}</b><br />"; ?>
            </p>
            <p class="message">
            <?php echo stripslashes($row['Message']); ?>
            </p>
            </div>
            </span>
<?php
} ?>
  • 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-05-16T11:36:30+00:00Added an answer on May 16, 2026 at 11:36 am

    Firstly – as Michael pointed out, you should be using a div for msg status:

    Second – assuming the example file is called test.php, you’re loading the entire content of the page back into the msgstatus sub-element (rather than just the SQL). You’d actually be doing this to infinity, except that browsers don’t allow executable code to be posted in an AJAX reply (so the second embedded tier is being ignored).

    I’m assuming you’ve simplified out of your code, but following is the updated code based on what you posted. NOTE there’s a bit of a style difference (feel free to ignore ;)!), you also included a BR in p.MESSAGEPOSTER when the paragraph was handling the newline (so I removed it).

    <?php 
    
    include 'config.php' 
    
    function latestPosts() {
        $query = "
        SELECT Users.UserID, Wall.Message, Users.Forename, Users.Surname 
        FROM Wall
        INNER JOIN Users ON Wall.UserID = Users.UserID
        ORDER BY Wall.MessageID DESC
        LIMIT 20;";
    
        $result = mysql_query($query) or die('Invalid query: ' . mysql_error());
    
        while ($row=mysql_fetch_assoc($result)) {
            echo "<div id=\"messagecontainer\">
        <img class=\"pic\" src=\"dummy.gif\">
        <p class=\"messageposter\">
            <b>".$row["Forename"]." ".$row["Surname"]."</b>
        </p>
        <p class=\"message\">
            ".stripslashes($row["message"]."
        </p>
    </div>\n";
        }
    }
    
    
    //Test for AJAX on the post - only output the values.
    if ($_REQUEST['ajax']==1) {
        latestPosts();
        exit();
    }
    
    
    ?>
    <script language="javascript">
    function createRequestObject() {
    
       var req;
    
       if(window.XMLHttpRequest){
          // Firefox, Safari, Opera...
          req = new XMLHttpRequest();
       } else if(window.ActiveXObject) {
          // Internet Explorer 5+
          req = new ActiveXObject("Microsoft.XMLHTTP");
       } else {
          // There is an error creating the object,
          // just as an old browser is being used.
         alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
       }
    
       return req;
    
    }
    
    // Make the XMLHttpRequest object
    var http = createRequestObject();
    
    function sendRequest(page,vars) {
    
       // Open PHP script for requests
       http.open('get', page+'?'+vars);
       http.onreadystatechange = handleResponse;
       http.send(null);
    
    }
    
    function handleResponse() {
    
       if(http.readyState == 4 && http.status == 200){
    
          // Text returned FROM the PHP script
          var response = http.responseText;
    
          if(response) {
             // UPDATE ajaxTest content
             document.getElementById("msgstatus").innerHTML = response;
          }
    
       }
    }
    
    
    function repeatloop()
    {
        sendRequest('test.php','ajax=1'); // replace "inbox-status.php" with your php page's url
        setTimeout("repeatloop()", 10000);
    }
    
    window.onload=function() {repeatloop();}
    </script>
    <link rel="stylesheet" href="style.css" type="text/css" />
    </head><body>
        <div id ="container">
            <div id="insideleft">
                <ul>
                    <li><a href="index.php">Home</a></li>
                    <li><a href="profile.php">Edit Profile</a></li>
                    <li><a href="wall.php">Community Wall</a></li>
                    <li><a href="logout.php">Logout</a></li>
                </ul>
            </div>
            <div id="insideright">
                <h1>Community Wall</h1>
                <br />
                <div id="postcontainer">
                    <form method="post" action="wall.php" name="wallpost" id="wallpost">
                        <input type="text" name="message" id="message" class="message" />
                        <input type="submit" name="messagesub" id="messagesub" value="Post Message" class="post"/><br /><br />
                     </fieldset>
                    </form>
                </div>
            <div id="msgstatus">
    <?php
    //Output the current posts - you may not even want to do this since AJAX will fill it in right after load
    latestPosts();?> 
            </div>
        <!-- ... we have some truncating! -->
    ?>
    

    Some other points:

    If you’re going with an AJAX solution, you might want to consider leaving the box empty in the starting HTML (drop latestsPosts(); at line 118), and then load the list once the page is rendered.

    The way this is currently coded, every 10 seconds the complete list is reloaded by AJAX, you may want to track the latest post (perhaps by ID?) and pass that back to the AJAX command so that either:

    1. An update is only provided when there’s been a change. (JS response processing would ignore an empty return – current code would overwrite status with blank)
    2. Only new entries are provided. (JS response processing would prepend the new response to the existing msgstatus object).

    Because this is a dynamic AJAX script you should be using the POST verb, not GET (it also requires some changes to how you post variables – the +'?'+vars in the http.open line needs to be in the body instead). Technically GET allows the browser to cache the results – I particularly notice this on Internet Explorer (ironically it seems they followed the W3C spec on this while the others don’t). Long story short – some users may complain the messages don’t stay up to date.

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

Sidebar

Related Questions

I have run the following code in this page RsyntaxTextArea using Java and i
I have this code: $('#page-refresh').click( function() { $.ajax({ url: /page1.php, cache: false, dataType: html,
I'm trying to run this code using tomcat in eclipse <%@ page language=java contentType=text/html;
I run this code here <html> <script type=text/javascript src=lib/jquery-ui-1.8.21.custom.min.js></script> <script src=http://127.0.0.1:5984/_utils/script/jquery.couch.js></script> <!--<script type=text/javascript src=lib/jquery-1.7.2.js></script>-->
I run this code: - (void)unitButtonButtonTapped:(id)sender { [_label setString:@Last button: Unembossed square]; MilitaryUnits *target
Why this code don't work,when i want run this code vwd 2008 express show
I'm trying to run this code but this error appear: Uncaught TypeError: string is
When I run this code, the output is 11, 10. Why on earth would
When I run this code to call the Google API, all I get is
When I run this code $sql_select = INSERT INTO `database`.`table`(Columns) VALUES (Values)... ; $mysqlid

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.