使用JavaScript实时更新网页元素时出错

I am working on a webpage that shows the amount of online players on a game server that I am running, that is updated in real time. The problem is that I can get the amount of players online in the game server to display, but it never updates and always shows the amount of players that were on the server when the page was loaded although people leave and join the server every second.

This is the PHP code that shows the numbers (it's simple, just for testing):

<?php
    echo "<a id='a1' href='#' class='online'>Loading...</a>";
?>

What I am doing is to update 'a1' every second with the new amount of online players using javascript, which calls a php function called getplayers():

<script language="JavaScript">
setInterval(function(){
    document.getElementById("a1").innerHTML = '<?php echo getplayers()?>';
}, 1000);
</script>

The function getplayers() it's exactly this:

<?php
include "Status.php";
function getplayers() {
    $serverb = new Status("mc.spainpvp.com", '25565');
    return $serverb->online_players;
}
?>

Lastly, Status.php is a script that gets the amount of players online and more things about the server, which I am sure that works:

<html>
<?php

class Status {

    public $server;
    public $online, $motd, $online_players, $max_players;
    public $error = "OK";
    function __construct($url, $port = '25565') {
        $this->server = array(
            "url" => $url,
            "port" => $port
        );
        if ( $sock = @stream_socket_client('tcp://'.$url.':'.$port, $errno, $errstr, 1) ) {
            $this->online = true;
            fwrite($sock, "\xfe");
            $h = fread($sock, 2048);
            $h = str_replace("\x00", '', $h);
            $h = substr($h, 2);
            $data = explode("\xa7", $h);
            unset($h);
            fclose($sock);
            if (sizeof($data) == 3) {
                $this->motd = $data[0];
                $this->online_players = (int) $data[1];
                $this->max_players = (int) $data[2];
            }
            else {
                $this->error = "Cannot retrieve server info.";
            }
        }
        else {
            $this->online = false;
            $this->error = "Cannot connect to server.";
        }
    }
}
?>
</html>

So my question is if someone knows why it always updates with the first number of players instead of putting the new number of players?

You can not call PHP functions by Javascript. PHP is processed on a server in time of request. No piece of PHP code will be visible in response, because it's already processed.

So your javascript code will actually look like:

<script language="JavaScript">
setInterval(function(){
  document.getElementById("a1").innerHTML = 'XXXXX';
}, 1000);
</script>

where XXXXX is amount of players in the time of request. So your code will every second replace elements innerHTML with the static content.

If you want to get new amount of players every second, you need to use Ajax. You can create request on your own using XMLHttpRequest or you may use some library like jQuery and it's $.ajax method. You also need a PHP code on a server that will provide such information.