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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:35:10+00:00 2026-05-26T07:35:10+00:00

I am new to js and jquery. The page I’m working on is a

  • 0

I am new to js and jquery. The page I’m working on is a message board. First the page displays 10 most recent messages and then my jquery function is used to update the board automatically, pulling new data every 2 seconds. The most recent messages are displayed on the top. The older messages should be removed from the page.

Please note that this is a simplified version of the page. I’m trying to work out the logic before implementing it to the actual page. Hence instead of the message, the id is showed etc.

<?php
include ("../../db.php");
 ?>
<!DOCTYPE html>
<html>
<head>

<script type="text/javascript" src="../jquery/jquery.js"></script>
</head>
<body>

<script>
function update() {
  $("#notice_div").html('Loading..'); 

  $.ajax({
    type: 'GET',
    url: '2include.php?lastid='+ latestid + '',
    timeout: 2000,
    success: function(data) {

      $("#cont_div").html(data);
      $("#cont_div").clone().prependTo($("#newdiv")); // adding the pulled data to newdiv 
      $("#notice_div").html(''); 
      window.setTimeout(update, 2000);
      // animate
      $("#newdiv").slideDown(1000,function(){ });

    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      $("#notice_div").html('Timeout contacting server..');
      window.setTimeout(update, 60000);
    }
});
}
$(document).ready(function() {
    update();

});
</script>

<div id="cont_div"></div>

<div id="newdiv"></div>

  <? 
  $count = 1; // first message is the newest
  $get = $DB->query("SELECT * FROM board ORDER BY id DESC LIMIT 10", __FILE__, __LINE__);
while ($msg = $DB->fetch_array($get))
{
    echo "<div id='msgid' style='background-color:yellow'>".$msg['id']."</div>";
  if($count == 1){ 
  $latestid = $msg['id']; // newest message - passing to js
  }
  $count++; 
} ?>
<script>
var latestid = <?=$latestid?>;
</script>

</body>
</html>

2include.php –

<?
include ("../../db.php");

 $la = $_GET['lastid'];

  $count = 1; // first message is the newest on load
  $get = $DB->query("SELECT * FROM board WHERE id>'$la' ORDER BY id DESC LIMIT 5", __FILE__, __LINE__);
while ($msg = $DB->fetch_array($get))
{
    echo "<div id='msgid' style='background-color:red'>".$msg['id']."";
  if($count == 1){ 
  $latestid = $msg['id']; // newest message
  }
  $count++; 
  ?>
  <script>
var latestid = <?=$latestid?>; // after pulling the newest message we overwrite latestid with the newest msg id
</script>
<? } ?>

result after inserting 3 new rows to the table after the first page load:

https://i.stack.imgur.com/rPf8N.jpg (sorry can’t post images yet)

the top red one is cont_div and appears every time new data is pulled for 2 seconds.

  1. What do you think of my code? I’m pretty new to jquery and not that good with javascript. This bit of code has taken me over 7 hours to build.
  2. Animation is not working (new messages should appear with the slide down animation.
  3. cont_div is displayed on the page (top red one on the image). Tried to put the content in variables or make the div invisible – nothing worked. If I made it invisible, the style would be copied to the new_div and it would be invisible there too.
  4. Only 10 messages should be displayed on one page. I haven’t figured out how to remove the older messages from the page.
  • 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-26T07:35:11+00:00Added an answer on May 26, 2026 at 7:35 am

    I’ll treat things like a call center and answer your questions in the order they were received:

    What do you think of my code?
    Overall, your Javascript code is very clean and well-organized. I usually put my Javascript code in external files instead of directly in the HTML, but that’s about all I have to suggest for the Javascript. One little thing I found is that you don’t have to specify a callback if you don’t want one. This is perfectly valid JQuery:

    $("#newdiv").slideDown(1000);

    Your PHP code is a little less organized, but you did say that you trimmed it down to post here, so I won’t comment on the formatting specifics. One thing I did notice, however, is that you have a security risk in this line:

    $la = $_GET['lastid'];

    What if lastid was set to 1'; DROP TABLE board;--? To guard against this, never use user input directly in a query. Since this value is an ID, you could say $la = intval($_GET['lastid']);, which will force the value to be an integer (strings will become 0). If you want to sanitize a string, check out mysql_real_escape_string.

    Animation is not working
    In HTML, ids are supposed to be unique. You should use a class if you want multiple elements to have the same value. In JQuery, you match an id with # and a class with .. It is probable that the browser is getting confused because there are multiple divs with the id newdiv. Additionally, all HTML elements are shown by default unless they are explicitly hidden. As a result, you’re telling the browser to slide down an element that is already being displayed, which is why it’s doing nothing. I’d recommend adding some CSS to newdiv to make it hidden by default (display: none;).

    cont_div is displayed on the page (top red one on the image). Tried to put the content in variables or make the div invisible – nothing worked.
    In your $.ajax call, I’d recommend adding dataType: "json" and not returning raw HTML in your AJAX function. This approach works better if you want to return status information (what if there was an error while accessing the database?), and will help you separate the content from the information. This way, you can design the entire web page in plain old HTML, and then add/remove HTML elements in Javascript based on the data returned from the server. I like to think of all AJAX calls as potential API calls from third parties. If someone else was to access that script, would they want your HTML too, or just the information?

    For more information on JSON, check out this quick tutorial: http://secretgeek.net/json_3mins.asp. You may not have known it, but you already made a JSON object in your $.ajax call.

    Only 10 messages should be displayed on one page.
    Like just about everything in the programming world, there are many ways to do this. One method would be to always prepend the new messages to the top of the list, and then remove the old ones with JQuery’s gt selector (http://api.jquery.com/gt-selector/). For example, you could add this line to your success function, which removes all div tags with the class msgid that have an index (position in their parent) greater than 5:

    $("div.msgid:gt(5)").remove();

    By the way, welcome to the web world! Keep playing around, and keep the questions coming.

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

Sidebar

Related Questions

I am new to JQuery And I have used 2 jQueries in my page.
i am new to jquery, i am working on a web page which needs
I'm new to jquery, I'm working on a web page which needs generating text
I m new with jquery.Below is my html table which is in content page.In
I am new with jQuery and I'm having a hard time getting my page
I'm pretty new to jQuery, and I am converting a page from regular javascript
I've been trying to use the new jquery-ui, and all but the resizable function
new to rails and really new to jquery. I've set up some AJAX page
I am new to jQuery. I have a button on main page of my
I'm struggling to determine why the addition of a new jQuery click function is

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.