doutang1873 2013-10-18 10:48
浏览 5
已采纳

如何在PHP中使用jQuery定期填充多个div?

Suppose I have an array which contains 2 itmes. Also I have 2 divs (mydiv1, mydiv2). First div has to be filled by first item of the array and second div has to be filled by the 2nd item of the array at the same time. And this process will repeat after regular intervals. I will keep on populating this array with new items by fetching from database, but that is out of scope of my question right now. How should I do this?

My xyz.php file is

<?php
//echo rand();
$questions=array(
                 "First Item",
                 "Second Item"
                 );
?>

index.php

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var auto_refresh = setInterval(
function ()
{
$('#mydiv1').load('xyz.php').fadeIn("slow");
}, 1000); // refresh every 1000 milliseconds
});
</script>
</head>
<body>
<div id="mydiv1"> </div>
<div id="mydiv2"> </div>
</body>

  • 写回答

2条回答 默认 最新

  • doubeijian2257 2013-10-18 10:54
    关注

    JS:

    var auto_refresh = setInterval(function() {
        $.getJSON('xyz.php', function(data) {
            $("#mydiv1").html(data[0]);
            $("#mydiv2").html(data[1]);
        });
    }, 1000);
    

    PHP:

    $questions = array("First Item", "Second Item");
    echo json_encode($questions);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测