douhao2548 2015-02-12 09:54
浏览 31

Ajax - 每秒检查数据库表值

I am attempting to check a value of a field in a database table every second and print it out in a html div. For some reason this is not working. The Inspector(F12) does not throw any error messages. Here is my code. If anyone can point me in the right direction it is highly appreciated:

php code:

echo("<input type='hidden' id='hidSessionOwner' value='".$userid."'/>");

Ajax code:

function check_invited_users(){
    $.ajax({
        type: 'POST',
        url: '/checkinvitedusers.php',
        data: {
            id: $('#hidSessionOwner').val()
        },
        dataType: 'json',
        success: function(response){
            if(console) console.log('Status = ' + response.status);
            if(response.status > 0){
            $('#divInvited').html(response);

            }
        }
    });
}
var check_search_status_interval;
$(document).ready(function(){
    check_search_status_interval  = setInterval(function() {
            check_invited_users();
    }, 1000);
});

And here is the html:

<div id="divInvited"></div>

and here is the code in the checkinvitedusers.php file:

$user_id = ($_POST['id']);

// Build SQL
$invite_search_sql = "select count from class_user_invites where user_id=".$user_id;
$result = mysql_query($invite_search_sql, $con) or die ("Error in query: $invite_search_sql " . mysql_error());
$row = mysql_fetch_array($result);
$response['status'] = $row[0];

echo json_encode($response);
  • 写回答

2条回答 默认 最新

  • dongyudun6465 2015-02-12 10:11
    关注

    Needed async: false option in the Ajax function.

    评论

报告相同问题?