使用脚本

时间:2016-08-08 06:20:31

标签: 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>

最后的脚本打印出第二个&#34; Prueba Mysql&#34;和&#34;连接成功&#34;,表只打印一次..
我怎么能这样做所以一切只在页面上?谢谢你

2 个答案:

答案 0 :(得分:0)

实际上是下面脚本的问题,每当加载页面时,setTimeout脚本都会触发并重新加载页面。

所以它再次循环,将验证检查到url已从脚本重定向。

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

答案 1 :(得分:0)

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