user2174050
user2174050

Reputation: 65

PHP refresh table without refresh entire page

I have a PHP page with one table that show any information in it. That information is refreshed every three secs. Now I've found this solution for refresh the table:

<?php
echo "<div>Welcome!</div>";
echo "<div align= \"center\" id=\"infos\">";    
echo "<table border= \"1\">";
...
echo "</table>";
echo "</div>";
?>

<script type="text/javascript">

$(document).ready (function () {
    var updater = setTimeout (function () {
        $('div#infos').load ('index.php', 'update=true').scrollTop(lastScrollPos);
        }, 3000);
});
</script>

in this way the html result is correct, the page is correctly updated every 3 secs, but after the first update happens a strange thing:

Html result:

<div>Welcome!</div>
<div align= "center" id="infos">    
<div>Welcome!</div>
<div align= "center" id="infos">    
<table border= "1">
...
</table>
</div>

So, all that I had before the refreshed div, now I have them inside the div. Where I'm wrong?

Upvotes: 4

Views: 13905

Answers (2)

H&#233;ctor William
H&#233;ctor William

Reputation: 796

If I'm not wrong you're calling the same file again so you could try to do this instead,

index.php

<?php
echo "<div>Welcome!</div>";
echo "<div align= \"center\" id=\"infos\">";    
echo "<table border= \"1\">";
...
echo "</table>";
echo "</div>";
?>

<script type="text/javascript">

$(document).ready (function () {
    var updater = setTimeout (function () {
        $('div#infos').load ('table.php', 'update=true').scrollTop(lastScrollPos);
        }, 3000);
});
</script>

table.php

<?php
echo "<table border= \"1\">";
...
echo "</table>";
echo "</div>";
?>

Upvotes: 3

anw
anw

Reputation: 160

seems that you load the result of index.php into div#infos

either make a php that ONLY generates the table you like to load or consider a general HTTP refresh - which will reload the entire page

Upvotes: 0

Related Questions