php - updating variable inside label without refresh -
simply, want display number of unread messages in label inside menu follows:
<li><a id="2" style="background-image:url('menu icons/comments.png'); background-repeat: no-repeat; background-position: left; background-position-x: 5px;" href= "viewmessages.php">messages<label id="num_msg">(<?php echo $count; ?>)</a></li> where $count as:
$num_messages = mysql_query("select count(m.message_id) cnt messages m inner join member_message_member mmm on (m.message_id = mmm.message_id) mmm.member_id2 = $id , m.seen = 0") or die(mysql_error()); $mcount = mysql_fetch_assoc($num_messages); $count = $mcount['cnt']; and when user clicks on message:
$("#sub a").click(function(){ mesg_id = $(this).attr('msg_id'); page = $(this).attr('href') id = $(this).attr('mid'); $.ajax ({ data: {message_id:mesg_id}, type: 'post', url: 'seen_messages.php', success: function(response) { if (response == 1) { } else { alert(response); } } }); }); seen_messages.php:
@mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("mydb") or die(mysql_error()); $message_id = $_post['message_id']; mysql_query("update `messages` set `messages`.`seen` = true message_id={$message_id}") or die (mysql_error()); echo "1"; everything working properly, except have refresh page update $count, there anyway update variable inside label without manual refresh?
make seen_messages.php respond updated unread count, instead of 1, use response update user interface:
// ... success: function(response) { if (isnan(response)) { alert('error!'); } else { $('#num_msg').text(response); } }
Comments
Post a Comment