javascript - 页面与脚本重复

标签 javascript php jquery html mysql

我正在为一个小项目学习 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/

相关文章:

javascript - node express react oauth 在回调中与 react 客户端应用程序授权后传递访问 token

php - 查询中的 COUNT() Likes 返回意外结果

jquery - 如何获取 "this"以下的元素

javascript - 去抖动无法正常工作

java - 带有问题网站的 HTTP 客户端 POST

javascript - 在文本框中按回车键打开链接

php - 如何以红色显示表单 errorSummary() ?

php - IF ELSE 不在 MySQL 中工作?

php - 使用 jQuery 在同一页面上显示表单并获取输入

javascript - 使用 anchor 到达页面时触发事件