简体   繁体   English

用PHP代码更新HTML标记

[英]Update HTML tag with PHP code

What would be the easiest way to update a HTML tag with the following php code? 用以下php代码更新HTML标签的最简单方法是什么?

<h1>
 <?php
    include "document.txt";
    ?>
    </h1>

What I mean is like a timed update... every 10 second or every 2 seconds. 我的意思是像定时更新...每10秒或每2秒更新一次。

Take a look at AJAX . 看看AJAX jQuery has a really easy to use function to make AJAX requests, see jQuery.ajax() . jQuery有一个非常易于使用的函数来发出AJAX请求,请参阅jQuery.ajax()

This example (modified from the jQuery page) is what you want: 您需要以下示例(从jQuery页面进行修改):

function loadData() {
    $.ajax({
      url: "document.txt",
      cache: false
    }).done(function( html ) {
      $("#results").html(html);
    });
}

It'll get the contents of the document.txt file and add it to an element with the attribute id="results" like the following: 它将获取document.txt文件的内容,并将其添加到具有id="results"属性的元素中,如下所示:

<h1 id="results"></h1>

You can then use window.setInterval to execute the code at a set interval. 然后,您可以使用window.setInterval以设定的时间间隔执行代码。

window.setInterval(loadData, 10000);

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

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