Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh a div with php data every 10 seconds using jQuery

Am trying to refresh data stored in a div every 10 seconds using jQuery.

My HTML code is:

<!DOCTYPE html>
<head>
<title>Untitled Document</title>

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>
    $(document).ready(function(){
        setInterval(function() {
            $("#latestData").load("getLatestData.php #latestData");
        }, 10000);
    });

</script>
</head>

<body>
    <div id = "latestData">

    </div>
</body>
</html>

And the PHP code I am using (temporarily as I know this won't change due to the same "data"):

<?php

    echo "test";

?>

However, it isn't even showing "test" on the html page.. could anyone suggest where I have gone wrong?

Many thanks

like image 839
New_programmer Avatar asked Apr 14 '13 12:04

New_programmer


2 Answers

Here's a way that will solve what you want to achieve, using the $.get method in jQuery:

$(document).ready(function () {
    setInterval(function() {
        $.get("getLatestData.php", function (result) {
            $('#latestData').html(result);
        });
    }, 10000);
});
like image 163
Jason Lipo Avatar answered Oct 05 '22 12:10

Jason Lipo


jQuery load method works in a different way. Try reading its documentation.

You don't have to specify destination element ID twice, remove the second one, like this:

$("#latestData").load("getLatestData.php");
like image 26
ulentini Avatar answered Oct 05 '22 12:10

ulentini