# Message count notification problem

**URL:** <https://www.phphelp.com/t/message-count-notification-problem/34027>\
**Category:** Javascript & Ajax\
**Created:** [October 2, 2021, 2:03am UTC](https://www.phphelp.com/t/message-count-notification-problem/34027 "2021-10-02T02:03:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kejo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@kejo](https://www.phphelp.com/u/kejo)\
**Post date:** [October 2, 2021, 2:03am UTC](https://www.phphelp.com/t/message-count-notification-problem/34027/1 "2021-10-02T02:03:30Z")

</div>

Greetings. I would like to know if anyone here is fluent enough with javascript to be able to assist me in correcting a notification problem. I am attempting to use javascript to access an external php file to select a message count from various users in a database on intervals. I am able to get it to work if there is ONLY one user. However it does not work for more than one. My print\_r() on the external php file does show all messages but the javascript apparently is the failure. I have tried to use:

var elem = document.querySelectorAll(’.counter’);  
var elem\_array = Array.prototype.slice.call(elem);

However, this did not show any data as it stated “undefined”.

My current javascript code is below which does work for only the first row of data from the database. Thank you in advance.

```auto
       function call_notification(elem) {        
        setInterval(function() {            
           var xml = new XMLHttpRequest();            
            xml.onreadystatechange = function() {
            if(this.readyState == 4 || this.status == 200) {
                const elem = document.querySelector('.counter');                   
                elem.textContent = this.responseText;   
                console.log(elem.textContent);                    
               }
          };
        
        xml.open("POST", "test.php", true);
        xml.send();             

           }, 5000);        
      }    
        
      call_notification();

Here is the external PHP file if anyone needs to see it.

        $me = $_SESSION['userid'];
        $sql = $database_connection->prepare("SELECT COUNT(`id`) AS `total` FROM `messages` WHERE `receiver` = :me");
         $sql->bindParam(':me', $me);    
         $sql->execute(array(
            ':me' => $me       
          )); 
          $result = $sql->fetchAll();
         //This print shows everything as it should
         //print_r($result);

        foreach($result as $item) { 
              $item['total'];   
        }  

      $total_new = $item['total']; 

        if($total_new > 0) {        
             echo $total_new; 
          }

```

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [October 3, 2021, 2:19am UTC](https://www.phphelp.com/t/message-count-notification-problem/34027/2 "2021-10-03T02:19:05Z")

</div>

That query should only ever return a single row, as long as the userID is in the session.

---

<div class="post-metadata">

**Author:** ![kejo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@kejo](https://www.phphelp.com/u/kejo)\
**Post date:** [October 3, 2021, 6:23pm UTC](https://www.phphelp.com/t/message-count-notification-problem/34027/3 "2021-10-03T18:23:41Z")

</div>

Thanks, I am looking to return all messages from each user who is NOT the current $\_SESSION(‘userid’) to the current $\_SESSION(‘userid’) who happens to be the receiver. Do you know what needs to be done to correct this?

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [October 4, 2021, 7:48pm UTC](https://www.phphelp.com/t/message-count-notification-problem/34027/4 "2021-10-04T19:48:33Z")

</div>

All messages or a count of all the messages?

---

<div class="post-metadata">

**Author:** ![kejo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@kejo](https://www.phphelp.com/u/kejo)\
**Post date:** [October 5, 2021, 1:31am UTC](https://www.phphelp.com/t/message-count-notification-problem/34027/5 "2021-10-05T01:31:24Z")

</div>

Thanks, I needed a count of all the pertinent unread messages. I was however able to figure it out, thanks. I am just working on refreshing the count now.
