简体   繁体   English

使用ajax,jquery更新多个HTML标签

[英]multiple HTML tag update with ajax, jquery

Having a basic understanding about AJAX ,JQUERY here is my code for updating an html tag and showing "date" shell command result on it every seconds. 对AJAX基本了解后,JQUERY是我的代码,用于更新html标记并每秒钟显示一次“ date” shell命令结果。

I am wondering how it is possible to expend this code and add some more tags and update them all. 我想知道如何扩展此代码并添加更多标签并全部更新。 eg i need to show cpu load, uptime,..... 例如,我需要显示CPU负载,正常运行时间.....

test2.php test2.php

<html>
    <head>
    <script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
    <script>
        $(document).ready(function() {
            setInterval(timestamp, 1000);
        });
        function timestamp() {
            $.ajax({
                url: '/test2.php',
                success: function(data) {
                    $('#timestamp').html(data);
                },
            });
        }
    </script>
</head>
<body>

<div id="timestamp">clock</div>
<div id="uptime">to be done!</div>

</body>
</html>

test2.php: test2.php:

<?php
    echo $timestamp=shell_exec('date');
    //echo $uptime=shell_exec('uptime -p');

?>

How about sending back JSON to your caller: 如何将JSON发送回您的调用方:

<?php

$data = [
     'timestamp' => shell_exec('date'),
     'uptime' => shell_exec('uptime -p')
     ];

echo json_encode($data);

?>

And then this (or something similar) in your Javascript ... 然后这(或类似的东西)在您的Javascript中...

success: function(data) {
     $('#timestamp').html(data.timestamp);
     $('#uptime').html(data.uptime);
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM