gpt4 book ai didi

javascript - 页面与脚本重复

转载 作者:行者123 更新时间:2023-11-29 20:35:19 26 4
gpt4 key购买 nike

我正在为一个小项目学习 PHP,并且使用 mysql 我制作了一个小表,该表每秒从 mysql 表中更新自己。

<!Doctype html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
<title>Pagina de prueba Mysql</title>
<style type="text/css">
table {
padding: 10pt;
margin:auto 10pt;
}
.present {
background-color: green;
font-size: 20pt;
}
.absent {
background-color: red;
font-size: 20pt;
}
</style>
</head>
<body>
<h1>Prueba Mysql</h1>

<?php
$servername = '192.168.0.101';
$username = 'dpto';
$password = 'n1nj4g41d3n';
$dbname = 'dpto';
// Crear conexion
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn -> connect_error) {
die('Connection failed> ' . $conn->connect_error);
}
else {echo 'Connection succesfull';}
print '<br>';
// Seleccionar weas
$sql_Jose = 'Select presente from dpto where id=2';
$sql_Lucas = 'Select presente from dpto where id=1';
$Lucas = $conn->query($sql_Lucas)->fetch_object()->presente;
$Jose = $conn->query($sql_Jose)->fetch_object()->presente;
if ($Lucas == 0) {$_Lucas = "<td class='absent'> Lucas </td>";}
else {$_Lucas = "<td class='present'> Lucas </td>";}
if ($Jose == 0) {$_Jose = "<td class='absent'> Jose </td>";}
else {$_Jose = "<td class='present'> Jose </td>";}


?>
<div id='tabla'>
<table>
<tr><?php echo $_Lucas ?></tr>
<tr><?php echo $_Jose ?></tr>
</table>
</div>
<script type="text/javascript">
$(document).ready (function () {
var updater = setTimeout (function () {
$('div#tabla').load ('index.php#tabla', 'update=true');
}, 1000);
});
</script>
</body>

最后的脚本打印出第二个“Prueba Mysql”和“连接成功”,表格只打印一次..
我怎样才能让所有内容都只在页面上出现一次?谢谢你的优势

最佳答案

$(document).ready (function () {
refreshTable();
});
function refreshTable(){
setTimeout (function () {
$('div#tabla').load ('index.php#tabla', 'update=true')
.always(refreshTable);
}, 1000);
}

关于javascript - 页面与脚本重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38822420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com