Reputation: 483
When I alert the userID, I get the correct number alerted from the else statement. The if statement won't alert at all. So I assume something is wrong with the condition i placed there.
The weird part is, even though the else statement, my div will not change color but it does alert correctly so I am confused.
AJAX function
$.ajax({
url: 'status.php',
dataType: "json",
type: 'GET',
success: function(data) {
if (data.message === "online"){
$('.status #user'+data.userId).css({background: '#40A547'});
} else{
//alert(data.userId);
$('.status #user'+data.userId).css({background: '#7f8c8d'});
}
}
});
status.php
header('Content-Type: application/json');
$array = array();
if (logged_in() === true){
$res8 = mysql_query("SELECT * FROM `users` LIMIT 1");
if(mysql_num_rows($res8) > 0){
while($row8 = mysql_fetch_assoc($res8)){
if ($row8['status'] === "1") {
$array['message'] = 'online';
$array['userId'] = $row8['user_id'];
}
}
}
}
else {
$array['message'] = 'offline';
$array['userId'] = '2'; // just for testing
}
echo json_encode($array);
The div
<div class="status" id="user2">test</div>
Upvotes: 0
Views: 98
Reputation: 183
My guess is that this block
if ($row8['status'] === "1") {
$array['message'] = 'online';
$array['userId'] = $row8['user_id'];
}
is never triggering. $row8['status'] is always 0 coming back from the database. Your fallback statement setting the message to offline doesn't trigger unless the database fails all together.
You should run this PHP test case:
header('Content-Type: application/json');
$array = array();
$array['message'] = 'online';
$array['userId'] = '2'; // just for testing
And confirm the JQuery fixes from above. Then go back to the database and verify the actual logged in case.
Upvotes: 1
Reputation: 10132
This is wrong:
$('.status #user'+data.userId)
----------^ <--- See space
You have no childNodes inside .status
Instead try this:
$('#user'+data.userId)
Also removed the .status
selector as the ID will be specific.
Upvotes: 1