本文實(shí)例講述了php+jQuery ajax實(shí)現(xiàn)的實(shí)時(shí)刷新顯示數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
創(chuàng)建數(shù)據(jù)表:demo
--
-- 表的結(jié)構(gòu) `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 ;
--
-- 轉(zhuǎn)存表中的數(shù)據(jù) `demo`
--
INSERT INTO `demo` (`id`, `name`) VALUES
(1, '雷軍'),
(2, '馬化騰'),
(3, '李彥宏'),
(4, '馬云');
服務(wù)器文件:demo.php
?php
$mysqli = new mysqli("localhost","root","","test");
$mysqli->set_charset('utf8');
$query = 'SELECT * FROM demo';
$result = $mysqli->query($query);
$arr = $result->fetch_all(MYSQLI_ASSOC);
$info = json_encode($arr);
echo $json = '{"success":true,"info":'.$info.'}';
顯示數(shù)據(jù)網(wǎng)頁: fresh.html
html>
head>
meta charset='utf-8'>
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;icount;i++){
var dom = "tr align='center' id='"+data.info[i].id+"'>td>"+data.info[i].id+"/td>td>"+data.info[i].name+"/td>/tr>";
var tag = '#'+data.info[i].id;
if(!$(tag).length){
$("#info").append(dom);
}
}
}else{
alert('error');
}
},
error:function(res){
alert(res.status);
}
});
}
window.setInterval(check, 1000); //每秒執(zhí)行一次
/script>
body>
div style='width:600px;margin:0 auto;'>
table border='1' width="600px">
thead>
tr>th>id/th>th>name/th>/tr>
/thead>
tbody id='info'>
tr align='center' id='111'>td>111/td>td>測試/td>/tr>
/tbody>
/table>
/div>
/body>
/html>
更多關(guān)于PHP相關(guān)內(nèi)容可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- jQuery+Ajax實(shí)現(xiàn)用戶名重名實(shí)時(shí)檢測
- Asp.net下利用Jquery Ajax實(shí)現(xiàn)用戶注冊(cè)檢測(驗(yàn)證用戶名是否存)
- jquery ajax 檢測用戶注冊(cè)時(shí)用戶名是否存在
- jquery+ajax實(shí)現(xiàn)異步上傳文件顯示進(jìn)度條
- Jquery ajax書寫方法代碼實(shí)例解析
- 基于ajax及jQuery實(shí)現(xiàn)局部刷新過程解析
- JQuery發(fā)送ajax請(qǐng)求時(shí)中文亂碼問題解決
- JQuery Ajax如何實(shí)現(xiàn)注冊(cè)檢測用戶名