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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:56:21+00:00 2026-05-30T05:56:21+00:00

I’d like to have a div on my web page that is based off

  • 0

I’d like to have a div on my web page that is based off of the data in my MySQL database. When the data in the database changes, the content in the div should change as well.

Example: Let’s say I have a field in the first row of a table called “server_statistics” in my MySQL database which keeps track of a server being online or offline. When the server goes offline, the MySQL field changes from 1 to 0. When it goes online, it goes from 0 to 1.

So I want to use Javascript to display the server status on my webpage and update it without refreshing the page.

I thought I’d be able to do it like this:

<script type="text/javascript">
    var int = self.setInterval("update()", 1000);
    function update() {
        var statusElement = document.getElementById("status");
        var status = "<?php 
            $con = mysql_connect("localhost", "root", "");
            mysql_select_db("database_name", $con);
            $row = mysql_fetch_array(mysql_query("SELECT * FROM server_statistics LIMIT 1"));
            mysql_close($con);
            echo $row[0]; ?>";
        if (status == 0) {
            statusElement.style.color = "red";
            statusElement.innerHTML = "offline";
        }
        else {
            statusElement.style.color = "green";
            statusElement.innerHTML = "online";
        }
    }
</script>

But this doesn’t work. The page needs to be refreshed for it to update and I don’t know why…

I’ve read I can use JQuery but I have no knowledge of this language at all.

I tried this:

<script type="text/javascript">
    var int = self.setInterval("update()", 1000);
    function update() {
        $('#status').load('load.php');
    }
</script>

and then in load.php I had this:

<?php
    echo "test";
?>

But nothing happened after 1 second passed. I’m probably doing it wrong because as I said, I don’t know anything about this JQuery/AJAX stuff.

So how can I accomplish retrieving a field from a MySQL database at every specified interval and then automatically update an HTML element accordingly? It would be ideal to only update the HTML when a change occurred in the database but for now, I just want it to change every few seconds or minutes…

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-05-30T05:56:23+00:00Added an answer on May 30, 2026 at 5:56 am

    What you need to do is utilize AJAX. Your page will need to make a request to the server each time it wants to check the status. There are two ways to do it: manually refresh the page yourself, or use an AJAX call.

    AJAX really isn’t all that difficult, you start by creating a separate PHP page check_status.php with the following in it:

    <?php 
        $con = mysql_connect("localhost", "root", "");
        mysql_select_db("database_name", $con);
        $row = mysql_fetch_array(mysql_query("SELECT * FROM server_statistics LIMIT 1"));
        mysql_close($con);
        echo $row[0]; ?>
    

    Then, in your HTML page, the easiest way to do this would be to use jQuery:

    var statusIntervalId = window.setInterval(update, 1000);
    
    function update() {
        $.ajax({
            url: 'check_status.php',
            dataType: 'text',
            success: function(data) {
                if (parseInt(data) == 0) {
                    $("#status").css({ color: "red" }).text("offline");
                } else {
                    $("#status").css({ color: "green" }).text("online");
                }
            }
        }
    }
    

    That’s it really. Every second, it will call the update() method which makes an AJAX call and inserts the result back into your HTML.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.