0

I am trying to use get_file_contents() in a setInterval() to repeatedly update some text which is displaying the contents of a file. Here's the code:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
  <div id="theDiv"></div>
</body>
<script>
$("document").ready(function(){
  setInterval(function(){
    $("#theDiv").text("<?php echo file_get_contents('file.txt'); ?>");
    console.log("<?php echo file_get_contents('file.txt'); ?>")
  },500);
});
</script>

The problem is: it seems that the text is not updating until I reload the page.

tshepang
  • 12,111
  • 21
  • 91
  • 136
felixpackard
  • 579
  • 7
  • 21
  • "The problem is, it seems that the text is not updating until I reload the page." -- PHP is interpreted **before** JavaScript. Once you understand you'll look back at this question and realize "oh, that doesn't make any sense" – elclanrs Dec 27 '13 at 03:51
  • 3
    Check the _generated_ HTML/JS to see why... – Passerby Dec 27 '13 at 03:51
  • PHP code is not in the HTML "just fine", see [How to call a PHP file from HTML or Javascript](http://stackoverflow.com/questions/20637944/how-to-call-a-php-file-from-html-or-javascript/20639432#20639432). – Theraot Dec 27 '13 at 04:09
  • You said you want to modify a file on the server [I would say that deserves to be another question]. Anyway, it has been asked already, give a look to [How to write into a file in PHP?](http://stackoverflow.com/questions/1768894/how-to-write-into-a-file-in-php) - by the way, take the [tour](http://stackoverflow.com/tour), at the moment of writing you haven't. – Theraot Dec 27 '13 at 04:15

3 Answers3

1

the file_get_contents() is NOT set to reload by setInerval. You mixed up client-side JavaScript & server-side PHP.

You probably need the following:

$("document").ready(function(){
    setInterval(function(){
        $("#theDiv").load('file.txt');
    },500);
});
Raptor
  • 53,206
  • 45
  • 230
  • 366
1

You have to create another php file to output the content and use ajax to get it.

Example:

$("document").ready(function(){
    setInterval(function(){
        $("#theDiv").load('/echo_the_content.php');
    },500);
});

Create echo_the_content.php with:

<?php

echo file_get_contents('file.txt');

If your file.txt is in public dir (web accessible), then you could load it directly without another php file.

xdazz
  • 158,678
  • 38
  • 247
  • 274
0

the setInterval get executed in the client side where as the php is evaluated in the server side so the command echo file_get_contents('file.txt'); is evalulated only once.

You need to use an ajax request where it returns the contents of the file

 $("#theDiv").load("file.txt");

where the server side resource file.txt returns the desired response that has to be displayed in the div

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • OK, so I'm now using $("#theDiv").load('file.txt');, and that's working great. The only other thing I need to know is, how can I update the text file when I click a button? I want to be able to click the button on my computer that's hosting the site OR my other computer, and have it update on both of them. What's the best way to do this? – felixpackard Dec 27 '13 at 04:10