2

I have a div element which call other php file output by php tag

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

I want to reload this div element without reloading whole page. Kindly help me. Thanks in advance.

0

2 Answers 2

4

This is using ajax, html, jquery, and php to make the contents of the div reload every five seconds. You need two files: index.php and livefeed.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>

Now every 5 seconds it will grab the contents from livefeed.php and put it in the div.

Sign up to request clarification or add additional context in comments.

9 Comments

can i have summary.php instead of index.php
this works but captioned div show my website index.php page. but i want to use this code on summary.php page
you can use summary.php instead of index.php
@ParvinderSingh please upvote this and mark as correct answer if it helped you
besides i want to refresh content very smoothly by using fade in or fade out function. How to do this.
|
0

Use AJAX. Since the question has a jQuery tag, here is the jQuery solution.

setInterval(function(){
    $(".View").load("sitename")
}, 20000)} //Every 20000 miliseconds (20 secs)

However, you will need to know the site name. You could inject it into the code but you would have to make sure you don't get a XSS (Cross-site-scripting) attack.

You should be able to securely pass all the data if you escape quotes and backslashes in the string. This is still quite dangerous, however.

I assume that the site=... shouldn't contain " or \. If so, you should remove these characters before placing them into the 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>

And then you can do

setInterval(function(){
    $(".View").load($(".View").data("update"))
}, 20000)} //Every 20000 miliseconds (20 secs)

What we have done is removed dangerous characters like " and \ and then made an AJAX request. Here is the full code.

<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>

5 Comments

sir i am a basic programmer, can u explain with details
The answer below works, but is very dangerous. You need to make sure that \ and " are filtered out of the URL before placing them into the JavaScript.
Ok, I have edited it. I cannot guarantee it is completely secure nor that it works.
i have tried above code. But the captioned div show example.com/index.php page
You can remove the quotes and backslashes, or replace them, or use the other answer but secure it with the methods I mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.