2

我写了这个简单的代码和这个连接到 mysql 数据库的表。它获取我在数据库中插入的数据,我想要的是页面自动刷新而无需刷新整个页面。
我的代码:

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

5 回答 5

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>";}
    ?>

page1.php 上的 Javascript(放在标题中):

<script type='text/javascript'>
   $(function(){
       $.get('page2.php',{},function(data){
           $('#tableID').replaceWith($(data));  
       });
   });
</script>
于 2011-06-29T14:09:49.513 回答
0

旁观

http://en.wikipedia.org/wiki/Ajax_%28programming%29

它会回答你的问题

于 2011-06-29T14:10:21.803 回答
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>

于 2011-06-29T14:10:37.247 回答
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
于 2011-06-29T14:14:36.530 回答
0

我对 PHP 所做的不多,但您可能希望 PHP 在其自己的单独页面上创建表格,然后在您想要刷新表格时使用 AJAX 获取数据。如果你使用道场,你可以做这样的事情。

索引.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>";}
    ?>
于 2011-06-29T14:29:22.940 回答