PHP / AJAX函数刷新

时间:2014-07-08 16:33:41

标签: javascript php ajax

我的功能有问题。 我想每5秒刷新一次这个功能而不刷新页面!

这是在refresh.php:

if ($files_count == 0) {         
        ?>       
    <img src="img/upload.gif">                 
    <?php    
   }

,这是在index.php:

<?php include 'refresh.php'; ?>

怎么做? :d

编辑:

1 个答案:

答案 0 :(得分:0)

<强> HTML

<div class="container"></div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<强>的JavaScript

setInterval(function(){check_files();}, 5000);

function check_files() {
   $.ajax({
      url: 'path/to/php/file.php',
      type: 'GET',
      cache: false,
      data: {},
      success: function (resp) {
         if (resp == 0) {
            $('.container').html('<img src="img/upload.gif" />');
         }
         else {
            /// Something else
         }
      }
   });
}

<强> PHP

<?php

$count = count(glob("your_directory/*",GLOB_BRACE)); // This is what I assume you're counting files with. You may be counting from a database, though. 

echo $count; // This goes back to the AJAX call.