dtjpnd7517 2012-07-01 22:39
浏览 53
已采纳

更新标签内的变量而不刷新

Simply, I want to display the number of unread messages in a label inside the menu as 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 i get $count as:

$num_messages = mysql_query("SELECT COUNT(m.message_id) as cnt FROM messages m INNER JOIN member_message_member mmm ON (m.message_id = mmm.message_id)
                    WHERE mmm.member_id2 = $id AND m.seen = 0") or die(mysql_error());
                    $Mcount = mysql_fetch_assoc($num_messages);
                    $count = $Mcount['cnt'];

and then when a user clicks on a 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 WHERE message_id={$message_id}") or die (mysql_error());
   echo "1";

everything is working properly, except that I have to refresh the page to update $count, So is there anyway to update the variable inside the label without manual refresh?

  • 写回答

2条回答 默认 最新

  • douyun1852 2012-07-01 22:43
    关注

    Make Seen_messages.php respond with the updated unread count, instead of always 1, then use that response to update the user interface:

    // ...
    success: function(response) {
       if (isNaN(response)) {
           alert('Error!');
       } else {
           $('#num_msg').text(response);
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?