0

I have a variable in a span tag:

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

My script:

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

"newMessageCnt.php" has one line:

<?php
include('header_application.php');
$pageValue = "dashboard";
$obj_clean->check_user();
echo $numOfMessages = $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']);    
?>
charlie_cat
  • 1,830
  • 7
  • 44
  • 73

2 Answers2

0

It seems that you are looking for $.load, and not $.data. Also, you may want to call it in interval or repeatable timeout:

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

 });   
</script>   
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • You mean $("msgcntDiv").load() ;) – Razvan Caliman Aug 25 '11 at 12:32
  • yeah, copy paste, and forgot to edit since my cellphone was ringing. Thanks :) – Maxim Krizhanovsky Aug 25 '11 at 12:35
  • Wouldn't using `setTimeout` recursively be a bit safer in this case? Suppose something went wrong on the server, it would prevent to keep on making requests. Also see: http://stackoverflow.com/questions/729921/settimeout-or-setinterval/731625#731625 – Geert Aug 25 '11 at 12:41
  • thanks all i have the $("msgcntDiv").load("newMessageCnt.php"); as: $("msgcntDiv").load(getUnopenedMessagesCount($_SESSION['user_id']); ?>); can i do this? other wise my .php will only have one line in it though – charlie_cat Aug 25 '11 at 12:50
  • @Helloise Smit You can't, as the PHP will be executed just one, to render the javascript. You need to call PHP file in order to get the new data. – Maxim Krizhanovsky Aug 25 '11 at 13:05
  • Helloise, you seem to be mixing JS and PHP there. My suggestion would be to keep newMessageCnt.php as a separate service that returns the count you need. – Razvan Caliman Aug 25 '11 at 13:06
  • thank you all i changed it back but it is not updating my variable $numOfMessages in the span?? – charlie_cat Aug 25 '11 at 13:14
  • @Helloise Smit that means you have fatal error in your PHP, but display_errors is off. Turn on display errors, or at least show the code. By the way, your PHP should not be one line long, as you have to initialize $obj_clean – Maxim Krizhanovsky Aug 25 '11 at 13:16
  • all ok now thank you but it is not updating my variable $numOfMessages in the span?? please see my code for newMessageCnt.php at the top – charlie_cat Aug 25 '11 at 13:20
  • You need to echo the $numOfMessages variable from your script. – Maxim Krizhanovsky Aug 25 '11 at 13:22
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> 

and file getUnopenedMessage.php, more or less:

<?php 
//session_start();
$obj_clean = new yourMailClass('...');
echo $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']) 
?>
roselan
  • 3,755
  • 1
  • 20
  • 20
  • probably setTimeout(refresh, 10000); ? – Maxim Krizhanovsky Aug 25 '11 at 13:50
  • it does call my .php but nope its not updating my variable in the – charlie_cat Aug 25 '11 at 13:59
  • does `$("#msgcntDiv").text(cnt);` do the trick? $.data() is a jquery function to set and get "private attributes" to dom elements of your choosing. I thougt you had another jquery function reading this cnt data in order to display the number of new mails. @Darhazer: yes! thanx! edited! ^^ – roselan Aug 25 '11 at 17:00