Home > Backend Development > PHP Problem > How to implement real-time data update in jquery php

How to implement real-time data update in jquery php

藏色散人
Release: 2023-03-14 18:10:01
Original
3237 people have browsed it

Jquery php method to implement real-time data update: 1. Create a data table; 2. Connect to the database by creating the server file "demo.php" and perform related operations; 3. Create "fresh.html" to display the data Just the web page.

How to implement real-time data update in jquery php

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

How does jquery php implement real-time data update?

php jQuery ajax implemented real-time refresh display data function example

The details are as follows:

Create data table: demo

--
-- 表的结构 `demo`
--
CREATE TABLE IF NOT EXISTS `demo` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) COLLATE utf8_bin NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=5 ;
--
-- 转存表中的数据 `demo`
--
INSERT INTO `demo` (`id`, `name`) VALUES
(1, '雷军'),
(2, '马化腾'),
(3, '李彦宏'),
(4, '马云');
Copy after login

Server file: demo.php

<?php
$mysqli = new mysqli("localhost","root","","test");
$mysqli->set_charset(&#39;utf8&#39;);
$query = &#39;SELECT * FROM demo&#39;;
$result = $mysqli->query($query);
$arr = $result->fetch_all(MYSQLI_ASSOC);
$info = json_encode($arr);
echo $json = &#39;{"success":true,"info":&#39;.$info.&#39;}&#39;;
Copy after login

Display data web page: fresh.html

<html>
<head>
  <meta charset=&#39;utf-8&#39;>
  <title>hello</title>
</head>
<body>
<script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script>
  function check(){
    $.ajax({
      type:"GET",
      url:"./demo.php",
      dataType:"json",
      success:function(data){
        if(data.success){
          var count = data.info.length;
            for(i=0;i<count;i++){
              var dom = "<tr align=&#39;center&#39; id=&#39;"+data.info[i].id+"&#39;><td>"+data.info[i].id+"</td><td>"+data.info[i].name+"</td></tr>";
              var tag = &#39;#&#39;+data.info[i].id;
              if(!$(tag).length){
                $("#info").append(dom);
              }
            }
        }else{
          alert(&#39;error&#39;);
        }
      },
      error:function(res){
        alert(res.status);
      }
    });
  }
  window.setInterval(check, 1000); //每秒执行一次
</script>
<body>
  <div style=&#39;width:600px;margin:0 auto;&#39;>
    <table border=&#39;1&#39; width="600px">
      <thead>
        <tr><th>id</th><th>name</th></tr>
      </thead>
      <tbody id=&#39;info&#39;>
        <tr align=&#39;center&#39; id=&#39;111&#39;><td>111</td><td>测试</td></tr>
      </tbody>
    </table>
  </div>
</body>
</html>
Copy after login

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to implement real-time data update in jquery php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template