简体   繁体   中英

I am trying to update part of my HTML every second and fetching data from database into that Div

I am trying to update part of my HTML every second and fetching data from database into that Div but the code is not working. :( Please help. The first section of code is html page where update function is called and ajax call is made on check_status.php and check_status.php fetches the data from php file from database

<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>
    }

You have a syntax error related to parentheses. Try this:

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
}

You should be able to fix syntax problems yourself with the help of errors reported on the javascript console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM