Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不重新加载页面的情况下重新加载调用php文件的Div_Php_Jquery_Ajax - Fatal编程技术网

在不重新加载页面的情况下重新加载调用php文件的Div

在不重新加载页面的情况下重新加载调用php文件的Div,php,jquery,ajax,Php,Jquery,Ajax,我有一个div元素,它通过php标记调用其他php文件输出 <div class="View"> <?php echo file_get_contents("http://example.com/livefeed.php?site=" . $_GET["site"] . "&num=6"); ?></div> 我想在不重新加载整个页面的情况下重新加载这个div元素。请帮帮我。提前感谢。这是使用ajax、html、jquery和php使div的内容

我有一个div元素,它通过php标记调用其他php文件输出

<div class="View"> <?php
echo file_get_contents("http://example.com/livefeed.php?site=" . $_GET["site"] . "&num=6");
?></div>


我想在不重新加载整个页面的情况下重新加载这个div元素。请帮帮我。提前感谢。

这是使用ajax、html、jquery和php使div的内容每五秒钟重新加载一次。您需要两个文件:index.phplivefeed.php

index.php:

<div class="View"></div>
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
<script>
setInterval(function(){
$.ajax({
            url: "livefeed.php?site=<?php echo $_GET["site"]; ?>&num=6",
            cache: false,
            success: function(html){        
                $(".View").html(html);          

            },
        });
},5000);
</script>

setInterval(函数(){
$.ajax({
url:“livefeed.php?site=&num=6”,
cache:false,
成功:函数(html){
$(“.View”).html(html);
},
});
},5000);

现在,每5秒钟它就会从livefeed.php获取内容并将其放入div中。

使用AJAX。由于问题有一个
jQuery
标记,下面是jQuery解决方案

setInterval(function(){
    $(".View").load("sitename")
}, 20000)} //Every 20000 miliseconds (20 secs)
但是,您需要知道站点名称。您可以将其注入代码中,但必须确保不会受到XSS(跨站点脚本)攻击

如果在字符串中转义引号和反斜杠,则应该能够安全地传递所有数据。然而,这仍然相当危险

我假设
站点=…
不应该包含
\
。如果是这样,您应该在将这些字符放入HTML之前删除它们

<div class="View" data-update="<?php echo "http://example.com/livefeed.php?site=" . str_replace("\\", '', str_replace('"', '', $_GET["site"])) . '&num=6">';
    echo file_get_contents("http://example.com/livefeed.php?site=" . $_GET["site"] . "&num=6");
    ?></div>

我们所做的是删除危险字符,如
\
,然后发出AJAX请求。这是完整的代码

<div class="View" data-update="<?php echo "http://example.com/livefeed.php?site=" . str_replace("\\", '', str_replace('"', '', $_GET["site"])) . '&num=6">';
    echo file_get_contents("http://example.com/livefeed.php?site=" . $_GET["site"] . "&num=6");
    ?></div>
<script>
    setInterval(function(){
        $(".View").load($(".View").data("update"))
    }, 20000)} //Every 20000 miliseconds (20 secs)
</script>

我可以用summary.php代替index.php吗?这很有效,但标题div显示了我的网站index.php页面。但是我想在summary.php页面上使用这段代码,您可以使用summary.php而不是index。php@ParvinderSingh请投票并标记为正确答案,如果它对您有帮助。此外,我想使用淡入淡出功能非常顺利地刷新内容。先生,我是一名基础程序员,你能详细解释一下吗下面的答案是可行的,但非常危险。您需要确保
\
在放入JavaScript之前从URL中筛选出来。好的,我已经编辑过了。我不能保证它是完全安全的,也不能保证它的工作。我已经尝试了上述代码。但是标题为div show example.com/index.php的页面您可以删除引号和反斜杠,或者替换它们,或者使用其他答案,但使用我提到的方法来保护它。
<div class="View" data-update="<?php echo "http://example.com/livefeed.php?site=" . str_replace("\\", '', str_replace('"', '', $_GET["site"])) . '&num=6">';
    echo file_get_contents("http://example.com/livefeed.php?site=" . $_GET["site"] . "&num=6");
    ?></div>
<script>
    setInterval(function(){
        $(".View").load($(".View").data("update"))
    }, 20000)} //Every 20000 miliseconds (20 secs)
</script>