What I'm trying to create right is basically a notification when someone messages you. It's simple, when I'm in the chat webpage and a friend message me a notification will appear next to his name. My problem is as the title suggest, where I'm finding difficulty passing a while loop variable from Php to a JavaScript function.
Here's my Php code:
<?php
$query = $db->prepare("
SELECT frst_user AS frst, scnd_user AS scnd
FROM frnd_rqst
WHERE (frst_user=:fid OR scnd_user = :fid)#
AND status=2
");
$query->bindParam(":fid", $getid);
$query->execute();
while($row = $query -> fetch(PDO::FETCH_ASSOC)) {
$numrow = $query -> rowCount();
$frst = $row['frst'];
$scnd = $row['scnd'];
$sec_quer = $db->prepare("
SELECT fname, lname, stud_id
FROM stud_bas
WHERE stud_id!=:sid
AND (stud_id=:fid OR stud_id = :tid)
");
$sec_quer->bindParam(":sid", $getid);
$sec_quer->bindParam(":fid", $frst);
$sec_quer->bindParam(":tid", $scnd);
$sec_quer->execute();
$result = $sec_quer->fetch();
$fname = $result['fname'];
$lname = $result['lname'];
$frid = $result['stud_id'];
?>
<ul>
<li class="friend-message" id="<?php echo $frid;?>" value="<?php echo $frid;?>">
<a href="#" ><?php echo $fname." ".$lname;?></a>
</li>
<p id="fridfrid<?php echo $frid;?>"></p>
</ul>
<?php
}
?>
Here's the JavaScript code:
<script type="text/javascript">
function numberOfMessages() {
var no_msg = new XMLHttpRequest();
var frid = "<?php echo $frid;?>";
no_msg.onreadystatechange = function() {
if (no_msg.readyState==4 && no_msg.status==200) {
document.getElementById('fridfrid' + frid).innerHTML = no_msg.responseText;
}
}
no_msg.open('GET','php/testing.php?frid=' + frid, true);
no_msg.send();
}
setInterval(function(){numberOfMessages()},1000);
</script>
I've tried putting the function inside the while loop but only works for the last output. I've also tried where you get all the names of but that doesn't seem to work for me also. Any suggestion?