dongzhong6675 2015-04-01 18:06
浏览 81
已采纳

无需刷新页面即可实时更新[关闭]

I would like to know the solution on how to make my PHP webpage/table update itself automatically in real time without having to refresh the page. The page works perfectly fine.

For example, If a user submits data this page will update itself automatically displaying the new users data. I have made research and it involves ajax. I have tried some attempts but it messes up the page. Any assistance would be kindly appreciated.

 <html>
<head>
 <title>Seminar Overview</title>
 </head>
  <h1><center>Seminar Overview</center>
    <body background="cloud.png">

 <h4><p><a href="add.php">Create Seminar</a></p><h4>
    <h4><p><a href="index.php">Admin Login</a></p><h4>
  <center>
   <?php
   //DISABLE ERRORS
      error_reporting(E_ERROR);
         //ESTABLISH DATABASE SERVER CONNECTION
       $con = mysql_connect ("194.81.104.22", "", ""); 
     if (!$con) {
   die("Can not connect: " . mysql_error()); 
}
 //MYSQL QUERY & DATABASE SCHEMA
mysql_select_db("db12408543", $con);
$sql = "SELECT * FROM Register";
$seminaroverview = mysql_query($sql,$con);
  $data = mysql_query($sql,$con);
 //TABLE
echo "<table border=4 table width=1000>
<tr>
 <th>Student ID</th>
 <th>Name</th>
    <th>Surname</th>
    <th>Telephone</th>
        <th>Address</th>
         <th>Date of Registration</th>
         <th>Email</th>
            <th>Registered Seminar(s)</th>
              </tr>";


    //MYSQL DATA TO BE DISPLAYED THROUGH PHP TABLE
      while($record = mysql_fetch_array($data)) {
        echo "<tr>";
         echo "<td>" . $record['idRegister'] . "</td>"; 
          echo "<td>" . $record['Name'] . "</td>"; 
         echo "<td>" . $record['Surname'] . "</td>"; 
            echo "<td>" . $record['Telephone'] . "</td>"; 
        echo "<td>" . $record['Address'] . "</td>"; 
             echo "<td>" . $record['Dateofregistration'] . "</td>"; 
             echo "<td>" . $record['email'] . "</td>"; 
                   echo "<td>" . $record['SeminarAttended'] . "</td>";
                    echo "</tr>";
                       }
             echo "</table>";

                  mysql_close($con);

                          ?>
                           </center>
                           </body>
                         </html>

Real time attempt

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script>
            $(document).ready(function() {
                setInterval(function() {  // set Interval function to carry out same operation in the time specified
                    $('#main').load('seminar-overview.php #main > *'); // Reloads 'seminar-overview.php' table every 6 seconds as <div> tag is specified and closed after table
            }, 6000);
                });
    </script>
  • 写回答

1条回答 默认 最新

  • dtnwm4807 2015-04-01 18:22
    关注

    OK, so after you populate your table for the first time you will need to send the form data to another PHP page via an ajax request. The example js code below (using jQuery) will execute a new PHP page with POST variables from a form named #updatePage. Then it will set the table contents to the pages output. Here it is:

    $('#updatePage').submit(function(event) {
        event.preventDefault();
        var form = $(this);
    
        $.ajax({
            url: "/path/to/script.php",
            data: form.serialize(),
            type: 'post',
            dataType: 'html',
            success: function(data) {
                $('table').html(data);
            },
            error: function(xhr, status) {
                alert("Sorry, there was a problem!");
            },
        });
    });
    

    The script.php might look something like this:

    <?php
    $newData = //MySQL functions to get data using POST vars
    
    echo "
        <tr>
            <td>" . $newData . "</td>
        </tr>
    ";
    ?>
    

    That's the basic idea and you should be able to modify your code to make this work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题