【问题标题】:How can I refresh a external div being called from a php file with ajax?如何刷新使用 ajax 从 php 文件调用的外部 div?
【发布时间】:2017-02-14 16:05:48
【问题描述】:

假设我有 2 个文件,index.php 和 get_data.php。在 get_data 中,我有一个 sql while 循环从我的数据库输出数据说用户名、图像和自上传以来图像的以秒/计时器为单位的年龄。在索引中,我想调用 get_feed 以显示 while 循环的输出。我只想刷新名为#timer 的计时器 div 而不是其余的数据。我怎样才能做到这一点?现在我只能刷新我在 index.php 中输出到 #response 的所有内容,其中包含图像,因此它会刷新图像,这些图像可能会导致性能下降。

index.php -

<div id="response"></div>

repeatAjax();
function repeatAjax(){
  $.ajax({    
    type: "GET",
    url: "get_feed.php",             
    dataType: "html",                 
    success: function(response){                    
        $("#response").html(response); 
    },
        complete: function() {
           setTimeout(repeatAjax,1000);
       }

    });
};

get_feed.php -

$today_date = date('Y-m-d H:i:s');
$sql = "SELECT image, user, date FROM images";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
   $image = $row['image'];
   $user = $row['user'];
   $date = $row['date'];

   $timeFirst  = strtotime($today_date);
   $timeSecond = strtotime($date);
   $timeSecond = $timeSecond + 86400;
   $timer = $timeSecond - $timeFirst;

   echo '<div id="timer">'.$timer.'</div>';
   echo '<div id="user">'.$user.'</div>';
   echo '<img src="'.$image.'" id="image"';
}
} else {
echo "0 results";
}

上面的脚本可能有错误,我只是为这个例子编写的,但我要做的只是每秒刷新一次计时器,而不刷新图像或用户名。我也担心如果服务器无法处理每秒刷新的负载,如果网站上有很多人的话,我的网站就会崩溃..

谢谢!

【问题讨论】:

  • 如果您的需求限制允许,您可以使用 JavaScript 来显示计时器,即使不访问服务器。
  • 我该怎么做?看起来我想做的事情很简单,也许我解释错了,这里有一个例子 - Facebook 喜欢按钮,当它点击按钮变成蓝色并且你的喜欢被添加到总数中并实时更新但它没有刷新帖子..我知道我没有点击任何东西,但似乎是同一个想法,@Rohith

标签: php jquery ajax while-loop refresh


【解决方案1】:

为避免重复,您不应从查询中获取所有数据,而应使用 where 子句作为日期,因为您使用 'Ymd H:i:s' 格式作为日期,而在 js 中只需附加 div

在 get_feed.php 中:

if(!empty($alreadyAccess)){
  $today_date = date('Y-m-d H:i:s');
  $sql = "SELECT image, user, date FROM images";
}
else{
  $sql = "SELECT image, user, date FROM images where date '$last_access_date'";
}

在 index.php 中将 .html 更改为 .append :

$("#response").append(response);

【讨论】:

  • 谢谢,但我没有关注这里,每个图像上都有一个计时器,因为它显示了该图像的生命周期,所以我想要每个图像、用户名和计时器的提要,但只需刷新计时器仅在第一次调用数据库后的每个图像。一旦我从数据库中提取日期,我应该能够运行计时器,因为该日期不会改变,只是当前时间会
【解决方案2】:

我认为这会有所帮助。

尝试替换

 success: function(response){                    
        $("#response").html(response); 
    },

success: function(response, status) {
        $("#response").html(response); 
    },

在你的 index.php 中

您修改后的 get_feed.php 以提供两个不同的 html 响应

 $timer=$_GET['tm'];
    $today_date = date('Y-m-d H:i:s');
    $sql = "SELECT image, user, date FROM images";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
       $image = $row['image'];
       $user = $row['user'];
       $date = $row['date'];

       $timeFirst  = strtotime($today_date);
       $timeSecond = strtotime($date);
       $timeSecond = $timeSecond + 86400;
       $timer = $timeSecond - $timeFirst;

       if($timer)
      {
      echo '<div id="timer">'.$timer.'</div>';
     }
     else
    {     
       echo '<div id="user">'.$user.'</div>';
       echo '<img src="'.$image.'" id="image"';
    }

    }
    } else {
    echo "0 results";
    }

你修改后的 index.php 来处理两个不同的 html 响应

<div id="response_wrapper">
<div id="timer_response"></div>
<div id="remaining_response"></div>
</div>

repeatAjax();
function repeatAjax(){
  $.ajax({    
    type: "GET",
    url: "get_feed.php",             
    dataType: "html",                 
    success: function(response, status) {                    
        $("#remaining_response").html(response); 
    },
        complete: function() {
           setTimeout(repeatAjax,3000);//comment this line of code if you want to prevent further refreshing of image and username
       }

    });
};
repeatAjax2();
    function repeatAjax2(){
      $.ajax({    
        type: "GET",
        url: "get_feed.php?&tm=true",             
        dataType: "html",                 
        success: function(response, status) {                    
            $("#timer_response").html(response); 
        },
            complete: function() {
               setTimeout(repeatAjax2,1000);
           }

        });
    };

【讨论】:

  • 我收到错误 -Uncaught TypeError: response.text is not a function
  • 谢谢,但这仍然会刷新整个 div #response 图片
  • 好吧抱歉我还是有同样的问题,静止图像刷新@Rohith
  • 它复制了 get_feed 的输出,一次刷新一次不刷新,但刷新时有图像和用户名
  • 是不是想加载图片并只使用一次名称?
猜你喜欢
  • 1970-01-01
  • 2013-06-24
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 2015-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多