I have a php code, which is in a div. It shows a random text placed in the random.txt file. I want to refresh this div, to load new text in every - let's say - 15 seconds without refreshing the whoe page.
I've found several solutions to reload divs without refreshing a page and it seems it can be done with AJAX or JS, but they only refresh it with a specific content or a specific file, however I can't figure out how to insert this code and have it refreshed.
This is how my div looks like:
<div id=randomtext>
<?php
include_once(GetRandomText.php);
$MPTextFile = random.txt;
$MPSepString = *divider*;
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>
</div>
It loads a random text to the div at every refresh.
I tried to refresh the div it with the JS below, but it's not working at all. Sorry, I'm not good in it.
<script>
var auto_refresh = setInterval(
function () {
var newcontent= '<?php
include_once(GetRandomText.php);
$MPTextFile = random.txt;
$MPSepString = *divider*;
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>';
$('#randomtext').html(newcontent);
}, 1000);
</script>
<div id=randomtext></div>
Thanks in advance.