使用ajax实时从数据库中获取数据?
我从以下数据库中获取数据:使用ajax实时从数据库中获取数据?
我有Arduino的框,发送数据。
并显示与此CSS & HTML代码中的数据:
<div class="event">
<img src="http://dummyimage.com/80x70/f00/fff.png" alt="picture" />
<p>Room 2</p>
<p class="patient-name">Jon Harris</p>
<p class="event-text">This is a pixel. A flying pixel!</p>
<p class="event-timestamp">feb 2 2011 - 23:01</p>
</div>
.event {
display:block;
background: #ececec;
width:380px;
padding:10px;
margin:10px;
overflow:hidden;
text-align: left;
}
.event img {
display:block;
float:left;
margin-right:10px;
}
.event p {
font-weight: bold;
}
.event img + p {
display:inline;
}
.patient-name {
display:inline;
color: #999999;
font-size: 9px;
line-height:inherit;
padding-left: 5px;
}
.event-text{
color: #999999;
font-size: 12px;
padding-left: 5px;
}
.event-timestamp{
color: #000;
padding-left: 5px;
font-size: 9px;
}
这是我的PHP代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DASHBOARD - Arduino 3</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?php
$con = mysql_connect("localhost","*****","***");
if(!con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("arduino_db",$con);
$result = mysql_query("SELECT * FROM events");
//Start container
echo " <div id='background_container'> ";
while($row = mysql_fetch_array($result))
{
echo "<div class='event'>";
echo "<img src='img/ev_img/red.jpg' alt='picture' />";
echo "<p>" . $row['inneboende'] . "</p>";
echo "<p class='patient-name'>" . "$row['overvakare']" . "</p>";
echo "<p class='event-text'>" . "$row['handelse']" . "</p>";
echo "<p class='event-timestamp'>" . "$row['tid']" . "</p>";
echo "</div>";
}
//end container
echo "</div>"
mysql_close($con);
?>
</body>
</html>
Arduino的箱发送数据到数据库..可以说,每3秒。我不想每5秒按一次F5来获取新数据。我应该使用AJAX吗?我已经阅读了一些网络上的AJAX,但我没有找到任何好的方法。
把它放在一个单独的php文件中,例如, getEvents.php:
<?php
$con = mysql_connect("localhost","*****","***");
if(!con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("arduino_db",$con);
$result = mysql_query("SELECT * FROM events");
while($row = mysql_fetch_array($result))
{
echo "<div class='event'>";
echo "<img src='img/ev_img/red.jpg' alt='picture' />";
echo "<p>" . $row['inneboende'] . "</p>";
echo "<p class='patient-name'>" . "$row['overvakare']" . "</p>";
echo "<p class='event-text'>" . "$row['handelse']" . "</p>";
echo "<p class='event-timestamp'>" . "$row['tid']" . "</p>";
echo "</div>";
}
mysql_close($con);
然后,负载jQuery和把下面的页面头部:
<script type="text/javascript">
$(document).ready(function() {
$.get('getEvents.php', function (data) {
$('#background_container').html(data);
});
});
</script>
注:这是不是你应该使用,看看确切的代码它并改变它,以便它能正确地为你工作。
谢谢,我有一个想法如何建立它..我在这里找到一个有用的技巧:http://bit.ly/mQlsPT – SHUMAcupcake 2011-05-16 21:32:59
如果你只是想刷新页面每隔5秒,你应该使用JavaScript和看的setTimeout:
setTimeout("location.reload(true);",5000);
5000等于5秒。
不,我只是想要更新页面的某些部分。 – SHUMAcupcake 2011-05-16 20:48:54
你仍然可以使用它只使第一个参数成为ajax函数。 – fanfavorite 2011-05-16 21:02:51
为什么这是低调的? – Ascherer 2011-05-16 21:08:16
@Ascherer我不知道,请upvote这个问题,我觉得你有用 – SHUMAcupcake 2013-01-06 14:52:50