2011-06-29 91 views
2

我寫了這個簡單的代碼和我連接到mysql數據庫的這張表。它提取我插入到數據庫中的數據,我希望頁面自動刷新,而不必刷新整個頁面。
我的代碼:Html自動刷新表

<html> 
    <head> 
     <title>Whats Up?</title> 
    </head> 
    <body> 
     <a href="Register.php">Register</a> <a href="login.php">Login</a> 


<?php 
    mysql_connect('localhost', 'root'); 
    mysql_select_db('summerinstitute'); 
    $query="SELECT * FROM students"; 
    $result=mysql_query($query); 
    echo "<table border=\"1\">"; 
    while ($row = mysql_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<td>"; 
    echo $row['fname']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['lname']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['username']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['Email']; 
    echo "</td>"; 
    echo "</tr>";} 
    ?> 
    </body> 
</html> 
+0

你試圖刷新只有表的結果嗎?也許在一個事件?你需要使用[AJAX](http://en.wikipedia.org/wiki/Ajax_%28programming%29)。 – wanovak

回答

1

您需要使用JavaScript和AJAX的一些實施僅refresh頁面的一部分。

例如用jQuery:

PHP page1.php中:

<html> 
    <head> 
     <title>Whats Up?</title> 
    </head> 
    <body> 
     <a href="Register.php">Register</a> <a href="login.php">Login</a> 
     <?php include_once('page2.php')?> 
    </body> 
</html> 

PHP使page2.php:

<?php 
    mysql_connect('localhost', 'root'); 
    mysql_select_db('summerinstitute'); 
    $query="SELECT * FROM students"; 
    $result=mysql_query($query); 
    echo "<table border='1' id='tableID'>"; 
    while ($row = mysql_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<td>"; 
    echo $row['fname']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['lname']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['username']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['Email']; 
    echo "</td>"; 
    echo "</tr>";} 
    ?> 

的Javascript上page1.php中(把它的報頭):

<script type='text/javascript'> 
    $(function(){ 
     $.get('page2.php',{},function(data){ 
      $('#tableID').replaceWith($(data)); 
     }); 
    }); 
</script> 
0

阿賈克斯

<script type="text/javascript"> 

     function Ajax() 
     { 
      var 
       $http, 
       $self = arguments.callee; 

      if (window.XMLHttpRequest) { 
       $http = new XMLHttpRequest(); 
      } else if (window.ActiveXObject) { 
       try { 
        $http = new ActiveXObject('Msxml2.XMLHTTP'); 
       } catch(e) { 
        $http = new ActiveXObject('Microsoft.XMLHTTP'); 
       } 
      } 

      if ($http) { 
       $http.onreadystatechange = function() 
       { 
        if (/4|^complete$/.test($http.readyState)) { 
         document.getElementById('ReloadThis').innerHTML = $http.responseText; 
         setTimeout(function(){$self();}, 10000); 
        } 
       }; 
       $http.open('GET', 'random.php' + '?' + new Date().getTime(), true); 
       $http.send(null); 
      } 

     } 

    </script> 

</head> 
<body> 

    <script type="text/javascript"> 
     setTimeout(function() {Ajax();}, 10000); 
    </script> 
    <div id="ReloadThis">Default text</div> 

</body> 

0

使用jQuery

setTimeout(function refreshTable() { 
$.ajax({ 
    url:'/some-script.php', 
    dataType:'html', 
    data:{ 
     someparam:'someval' 
    }, 
    success:function(data) { 
     $('#yourTable').find('tbody').empty().append(data); 
     setTimeout(refreshTable, 5000); 
    } 
}); 
}, 5000); //every 5 seconds refresh 
+0

我喜歡使用$ .ajax而不是$ .post或$ .get –

+0

這會在5秒後刷新表格,一旦ajax請求完成 –

0

我對PHP沒有太多的工作,但是您可能希望PHP能夠在自己單獨的頁面上創建表,然後在您想刷新表時使用AJAX獲取數據。如果你使用道場,你可以做這樣的事情。

的index.html

<html> 
<head> 
    <script type="text/javascript"> 
    //call this function whenever you want to update the table 
    function updateTable() 
    { 
     dojo.xhrGet({ 
      url: 'my_table.php', 
      handleAs: 'text', 
      load: function(data) { 
       dojo.byId('table').innerHTML = data; 
      }); 
    } 
    </script> 
    <title>Whats Up?</title> 
</head> 
<body onload='updateTable()'> 
    <a href="Register.php">Register</a> <a href="login.php">Login</a> 

<div id='table'></div> 
    </body> 
</html> 

my_table.php

<?php 
    mysql_connect('localhost', 'root'); 
    mysql_select_db('summerinstitute'); 
    $query="SELECT * FROM students"; 
    $result=mysql_query($query); 
    echo "<table border=\"1\">"; 
    while ($row = mysql_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<td>"; 
    echo $row['fname']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['lname']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['username']; 
    echo "</td>"; 
    echo "<td>"; 
    echo $row['Email']; 
    echo "</td>"; 
    echo "</tr>";} 
    ?>