admin管理员组

文章数量:1293033

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.

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.

Share asked Jan 16, 2014 at 23:18 takacsbalazstakacsbalazs 31 gold badge1 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

PHP Script and Javascript are executed in separate environments. (Your server and user's browser, respectively.)

What you need to do is to move the embedded PHP script into a separate PHP file. And using somthing like this:

content.php

<?php
include_once("GetRandomText.php");
$MPTextFile = "random.txt";
$MPSepString = "*divider*";
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>

Inside your html page

<script>
var auto_refresh = setInterval(
(function () {
    $("#randomtext").load("path/to/content.php"); //Load the content into the div
}), 1000);
</script>
<div id="randomtext"></div>

Document for jQuery Load function: http://api.jquery./load/

As far as I understand you, you are trying to execute your PHP in a browser which is not possible. Your server parses the PHP and replaces that code with the random string.

You need a AJAX request to your server which evaluates this script and sends it back to the client. In a simplified way:

request = new XMLHttpRequest();
request.open("GET", "my.php");
request.send();
$("#randomtext").html(request.response);

本文标签: javascriptRefresh a div which contains a PHP codewithout refresing the pageStack Overflow