Reputation: 47
I am trying to load php file's output in html div. And I have tried it with jQuery's ($('div').load(url_here))
method and it's working fine, but my php file is as below:
<?php
for( $i = 0 ; $i < 10 ; $i++ )
{
echo date('h:i:s').'<br>';
//flush the output buffer
flush();
//send the output buffer
ob_flush();
//sleep here
sleep(1);
} ?>
Now this php file prints a single line every second, but when I load this php in html's div, it takes 10 seconds once the page is loaded and then adds 10 lines all together. I want to load div as php file returns a single line every second. I mean, I want html to respond same as php (i.e. load one line every second, so at 1st second it should load 1st line, at 2nd second it should add 2nd line and so on). So, is it possible? (P.S - I am beginner for this dynamic web)
Upvotes: 0
Views: 422
Reputation: 21465
In order to get the result in every minute, you have to open an ajax request every minute to control the time in javascript, not in php.
Try:
var timer = window.setInterval(function()
{
$('div').load(url_here);
}, 1000);
And remove the for
loop and the sleep()
in php.
UPDATE:
To keep the history of the results, try this out:
var content = $('div').text();
$('div').load(url_here).append(content);
Upvotes: 1
Reputation: 36703
PHP
<?php
echo date('h:i:s').'<br>';
?>
And JS
<script>
var timer_php = setInterval(function(){
$("#anydiv").load("abovefile.php");
},1000);
setTimeout(function(){
clearTimer(timer_php);
},10000);
</script>
Upvotes: 0