My notification sender is working perfectly. I can see the notification when I refreshed the page, without the AJAX.
I want to implement AJAX to refresh the content automatically when there is new notification is incoming for the user.
Example for what I am trying to achieve:
user A
sent a friend request to user B
, so user B
gets a notification, but the notification is only showing after the page is refreshed.
I want to refresh the notification automatically, whenever the database is updated/inserted a new value.
$(document).ready(function(){
$.ajax({
type: 'POST',
url: '../main/count-fnd.php',
success: function(data){
$('#output').html(data);
}
});
});
The problems that I have had with AJAX reload:
The Ajax refresh won't stop refreshing
If I put a button next to the
<div id='output'></div>
(not inside of the div tag) tag, the button won't stop refreshing with the div element itself.
Code for fetching the notification:
$query = "SELECT * FROM notrequests WHERE userTo = '$userName' AND counter != '0'";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$totalRequestNot = $row["counter"];
}
}