Reputation: 53
I made simple page with slides and wanted to load it every 5 sec when JS timer looping.
I add comments which describe where the problem is. PHP scan dir only when page loads and does not notice changes in the folder in timer.
here's the code:
<script type="text/javascript">
......
var main_timer = setInterval(function(){slideTimer()}, 5000);
......
function slideTimer() {
......
<?php
//This PHP code need to be looped inside the timer
$array = array();
$array = array_slice(scandir('strony'), 2);
$js_array = json_encode($array);
?>
........
pages = <?php echo $js_array; ?>;
........
........
}
</script>
Upvotes: 1
Views: 2162
Reputation: 14855
Here you have to understand the concept of server side scripting and client side scripting.
Java script is a client side scripting language.
PHP is a server side language.
In order to execute the Server Side Language you have to execute the code in the server, where PHP can interpret it.
In your case you are trying to loop a client script in javascript and adding the PHP codes inside the client script.
You have to note, when the web server present the whole page, the PHP script was already executed, so looping the PHP codes inside javascript will not interpret it again as the client (your browser) doesnt have a PHP interpreter inbuilt.
Solution
The timer from javascript should call a method in the server using AJAX
and then in your server side ajax page, you have to include
Ajax call from JQuery
function slideTimer() {
.......
.......
$.post('ajax.php', function(data){
pages = data
if(current_page < pages.length - 1) {
........
........
},'json');
}
File ajax.php
<?php
// here NOT work
$array = array();
$array = array_slice(scandir('strony'), $_POST['page']);
$js_array = json_encode($array);
header('Content-Type: application/json');
echo $js_array;
?>
Here the server script will echo back the js_array
Upvotes: 1