我可以这样做吗? $( “msgcntDiv”)的数据( “newMessageCnt.php”)。

时间:2011-08-25 12:27:36

标签: jquery

我在span标签中有一个变量:

 echo "<span class='headings_sub' id='msgcntDiv'>You have ".$numOfMessages." </span>";

我的剧本:

 <script>
    $(document).ready(function() {
         var reload = function(){  
       $("msgcntDiv").load("newMessageCnt.php");
    }
    window.setInterval(function() {
        $("#msgcntDiv").load(reload);
    }, 10000);
     });   
</script>   

“newMessageCnt.php”有一行:

<?php
include('header_application.php');
$pageValue = "dashboard";
$obj_clean->check_user();
echo $numOfMessages = $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']);    
?>

2 个答案:

答案 0 :(得分:0)

您似乎在寻找$ .load,而不是$ .data。此外,您可能希望在间隔或可重复的超时中调用它:

 <script>
$(document).ready(function() {
    var reload = function(){
       $("msgcntDiv").load("newMessageCnt.php"); 
    }
    window.setInterval(reload, 10000);

 });   
</script>   

答案 1 :(得分:0)

 <script>
    $(document).ready(function() {
        refresh();
    });

    function refresh()
    {
        $.get('getUnopenedMessage.php', function (cnt) {
            // $("#msgcntDiv").data('cnt', cnt);
            $("#msgcntDiv span").text(cnt);
            setTimeout(refresh(), 10000);               
        });
    }
</script> 

和文件getUnopenedMessage.php,或多或少:

<?php 
//session_start();
$obj_clean = new yourMailClass('...');
echo $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']) 
?>