I'm building push notifications for my messaging system and have a weird bug.
I'm using an ajax to get recent messages. In my PHP script I have a while loop where I go through my results. So each <li>
is a 'recent message'.
In my mind it would be simple. I put an ajax function in the <li>
and as it iterates through the while loop it will send the values received from the iteration. Below is my PHP script.
$output .= "
<li>
<img src='$profilephoto' class='rm_pp' alt=''>
<div class='imNotification'>
<script>
function getIMNotification() {
$.ajax({
url: 'getIMNotification.php',
method: 'POST',
data:{user2:'$id'},
success:function(data) {
$('.imNotification').html(data);
}
});
}
getIMNotification();
</script>
</div>
</li>
";
For example, in my getIMNotification.php
if i just echo the user2
value sent from my AJAX, it will echo the same value for each result. But, since it's in the while loop, shouldn't it receive new values each iteration?
Is it because of the function being called? The one value being echoed is the last id
in the loop. Any logic as to why it's doing that?