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 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题