首页 文章

我试图每秒更新一部分HTML并将数据从数据库中提取到Div中

提问于
浏览
0

我试图每秒更新我的部分HTML并从数据库中获取数据到该Div但代码不起作用 . :(请帮助 . 代码的第一部分是调用更新函数的html页面,并在check_status.php上调用ajax,check_status.php从数据库中获取php文件中的数据

<html>
<head>
<title>Reloading testing</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
var statusIntervalId = window.setInterval(update, 1000);

function update() {
    $.ajax({
        url: 'check_status.php',
        dataType: 'text',
        success: function(data) {
            if (parseInt(data) == 0) {
                $("#status").css({ color: "red" }).text("offline");
            } else {
                $("#status").css({ color: "green" }).text("online");
            }
        }
    }
}

</script>
</head>

<body>
    <h1>Refresh part of the page</h1>
    <div id="status">
    </div>  
</body>
</html>


This is check_status.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    </head>
<body>
<?php 
    $con = mysql_connect("localhost", "root", "");
    mysql_select_db("manchesterunited", $con);
    $row = mysql_fetch_array(mysql_query("SELECT * FROM players LIMIT 1"));
    mysql_close($con);
    echo $row[0]; ?>
</body>
</html>
    }

1 回答

  • 0

    您有与括号相关的语法错误 . 试试这个:

    function update() {
        $.ajax({
            url: 'check_status.php',
            dataType: 'text',
            success: function(data) {
                if (parseInt(data) == 0) {
                    $("#status").css({ color: "red" }).text("offline");
                } else {
                    $("#status").css({ color: "green" }).text("online");
                }
            }
        });    // properly end the ajax() invocation
    }
    

    您应该能够借助javascript控制台上报告的错误自行修复语法问题 .

相关问题