实时更新,无需刷新页面
Real time update without refreshing the page
我想知道如何在不刷新页面的情况下使我的PHP网页/表自动实时更新的解决方案。这一页写得很好。
例如,如果用户提交数据,此页面将自动更新自身,显示新用户的数据。我已经做了研究,它涉及ajax。我试过几次,但把页面弄乱了。如有任何协助,我们将不胜感激。
<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>
实时尝试
<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>
OK,所以在第一次填充表之后,您需要通过ajax请求将表单数据发送到另一个PHP页面。下面的示例js代码(使用jQuery)将使用名为#updatePage
的表单中的POST变量执行一个新的PHP页面。然后它会将表内容设置为页面输出。这是:
$('#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!");
},
});
});
script.php可能看起来像这样:
<?php
$newData = //MySQL functions to get data using POST vars
echo "
<tr>
<td>" . $newData . "</td>
</tr>
";
?>
这是基本的想法,您应该能够修改您的代码以使其工作。
相关文章:
- 标记的实时更新,无需加载页面谷歌地图API V3
- 使用Ajax调用在服务器上实时更新页面
- 实时更新,无需刷新页面
- Dojo dGrid/dStore实时更新
- 在内容刷新或更新时调整iframe的大小
- 如何在页面上进行实时更新
- 从JSON实时更新
- 我希望我的php/html网站上的JavaScript能够实时更新
- 使用php-js-msyql的实时更新消息系统
- 实时更新的持久连接
- Node js redis socket.io pubsub实时更新
- 在OpenCart中,我们如何实时更新数据
- Instagram实时更新没有回应
- 在react组件中使用ajax获取实时更新的数据
- 在Javascript中刷新按钮更新Accordion
- 如何使用highchart在yii中添加实时更新图表
- 将实时更新传递到条纹结账金额
- JavaScript 从 1 个文本区域实时更新到多个文本区域
- 在 PHP / Javascript 中实时更新时间
- 在用户不刷新时更新 Web 应用程序