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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:05:42+00:00 2026-05-24T08:05:42+00:00

I have two PHP scripts. One acts as a backend, and contains a class

  • 0

I have two PHP scripts. One acts as a backend, and contains a class definition with some functions in it that show things like the computer’s load average and RAM usage. The other acts as a frontend, which calls those functions and prints them along with some HTML and CSS to make it look nice. This works fine, but it doesn’t update itself unless you refresh the page.

I made the backend able to return JSON tables of one function at a time, using the following code:

<?php    
if (!empty ($_GET['q']))
{
    $status = new status();

    $query = $_GET['q'];
    $data = $status->$query();

    exit (json_encode($data));
}
?>

And I receive the data using JavaScript like so: (I am not using any frameworks)

function update_load()
{
    var prefix = "Load average: ";

    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            var json = JSON.parse(xmlhttp.responseText);
            document.getElementById("loadavg").innerHTML = prefix + json.load;
        }
    }
    xmlhttp.open("GET", "./backend.php?q=load", true);
    xmlhttp.send();

    setTimeout ("update_load()", 10000);
}

The JSON response the page gives is valid:

{"model":"Intel Xeon 5150 @ 2.66GHz","load":"0.00 0.02 0.05"}

I have another function update_ram() which is nearly identical and runs at exactly the same time. Both of these functions run on page load and work fine usually on the first time they run. Sometimes though, one of them (usually the load average) will stop working and instead display “undefined” and never update itself again. I put in an alert (xmlhttp.response) into both functions, and the backend script looked like it was sending the table for the update_ram() to both of the functions.

I tried delaying one of the functions by one second on page load, so now this problem happens less often, but it still happens. I suppose I could have it return the functions I want all at once, but then it would lose some reusability. What could be causing this and what can be done to fix it?

Or, is there an entirely different way I should be doing this?

  • 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-24T08:05:43+00:00Added an answer on May 24, 2026 at 8:05 am

    If you are using the same global xmlhttp variable to hold the reference to both XmlHttpRequests, then naturally one of the statechange actions is going to be confused. Make it a local variable instead with

    function update_load()
    {
       var xmlhttp ;
       ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two scripts that I call with two PHP include() calls. The first
I have a large amount of data to move using two PHP scripts: one
I have two autocomplete scripts, one of them makes use of (A) php/mysql/html/jquery and
I have two scripts one in PHP to get the time server side, and
I have two scripts that are identical but one works on my home testing
I have two PHP scripts, both using the same session by calling session_name('MySessID') .
I have two PHP files that I need to link. How can I link
I have two PHP functions to calculate the relation between two texts. They both
I am confused with & and && . I have two PHP books. One
I have two classes in a PHP application, B extending A for example. Class

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.