0

What would be the easiest way to update a HTML tag with the following php code?

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

What I mean is like a timed update... every 10 second or every 2 seconds.

4
  • 2
    When? After it has already rendered to the browser? If so, you need an AJAX request to call document.text. Commented Jun 28, 2012 at 23:54
  • I have already written something like that in a function for a button! What it does is to run a PHP file. Commented Jun 28, 2012 at 23:55
  • In the simplest case $("h1").load(".../update.php"); Commented Jun 29, 2012 at 0:05
  • 1
    first of all: We trust you, you do not need to defend yourself. It was asked for more information, nothing else. It's just not clear what you ask. And your comment didn't add much more information. Personally I would like to see a picture of you, but you don't need to post that. Commented Jun 29, 2012 at 0:05

1 Answer 1

1

Take a look at AJAX. jQuery has a really easy to use function to make AJAX requests, see jQuery.ajax().

This example (modified from the jQuery page) is what you want:

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:

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

You can then use window.setInterval to execute the code at a set interval.

window.setInterval(loadData, 10000);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.