objelland
objelland

Reputation: 119

Ajax - Check database table value every second

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);

Upvotes: 0

Views: 1876

Answers (2)

sheriff
sheriff

Reputation: 17

USE ASYNC FLASE OPTION AND TRY UP. i think you can solve this problem.
function check_invited_users(){ $.ajax({ type: 'POST', url: '/checkinvitedusers.php', data: $("#hidSessionOwner").serialize(), async: false

just try this and see.i think it is a solution for your question.

Upvotes: 1

user247813
user247813

Reputation: 81

Needed async: false option in the Ajax function.

Upvotes: 1

Related Questions