dongxuandong2045 2014-07-21 13:48
浏览 45
已采纳

在该div中仅重新加载<DIV>和php代码

I have below code in a page called home.php.

<div class="Wrapper-notify" id="Wrapper-notify">
           <?php  echo rand(10,100); ?>
    </div>

How can I reload only this div and the php code on regular time interval. (I'm using codeigniter framework) please help

  • 写回答

7条回答 默认 最新

  • dsfs1233 2014-07-21 14:30
    关注

    You should first create a .php page, let's say test.php printing data like this :

    <?php
        echo rand(10,100);
    ?>
    

    An you can use this code to load data from your page, this one is pure javascript

    function getValue()
    {
        if (window.XMLHttpRequest)
            AJAX=new XMLHttpRequest(); 
        else
        AJAX=new ActiveXObject("Microsoft.XMLHTTP");
        if (AJAX)
        {
            AJAX.open("GET", "test.php", false);
            AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            AJAX.send();
            return AJAX.responseText;                                         
        } 
        else
            return null;
    }
    window.onload=function()
    {
        setInterval(function(){
        document.getElementById("Wrapper-notify").innerHTML = getValue();
        },1000);
    };
    

    However, if you are using jQuery you can the use .get method to load data from server

    $(document).ready( function() 
    {
        setInterval(function(){ changeDiv(); },1000);
    });
    function changeDiv()
    {
        $.ajax({
             url: "test.php",
             success: function(result)
             {
                $("#Wrapper-notify").html(result);
             }
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真