Ajax request not working, why?

Asked

Viewed 123 times

0

With this script I can show a counter next to <i class="fas fa-bell mr-3"></i> when you have data in db marked as status = unread, and when I click on the icon <i class="fas fa-bell mr-3"></i> updates certain Row to status = read.

When I enter data into my table with the status = unread the $countNT is incremented, until there is working perfectly and this $countNT is incremented without updating the page.

When I click on <i class="fas fa-bell mr-3"></i> I send a POST request with Ajax to the file update.php, and I can update the Row that used to be status = unread now would be status = read and the $countNT Zera.

The problem is that the request with Ajax for the file update.php is only running once on the page, example: if I updated the page about 10s ago, and then there is a notification, I can update the table in real time without having to refresh the page, but if I had already called the file update.php with Ajax on that page and then comes another notification, it will not be possible to update my database, I can only update the database if I reload the page. Can someone explain to me why?

html:

<div>
    <ul class="navbar-nav textoPerfilDesk dropMenuHoverColor">
        <li class="nav-item dropdown pr-2 dropleft navbarItem ">
        <a class="nav-link dropdown-toggle-fk" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fas fa-bell mr-3"></i>
        </a>
        <div class="dropdown-menu dropdown-menu-fk py-3" aria-labelledby="navbarDropdownMenuLink">
            <a class="dropdown-item dropMNitemNT" href="um-link">
            <span class="d-flex">
                <img class="imgNT" src="img/1.jpg">
                <span class="pl-2 pt-1">
                    titutlo
                </span>
            </span>
            </a>
        </div>
        </li>
    </ul>
    <span class="text-white divCountNT" id="datacount"></span>
  </div>

script:

<script>
  $(document).ready(function(){

    $("#datacount").load("select.php");

    setInterval(function(){
    $("#datacount").load('select.php')
    }, 1000);

  });

  $(document).on('click', '.fa-bell', function (){

    $.ajax({
       type: "POST",
       url: "update.php"
    }).done(function() {
      $("#datacount").load("select.php");
    });

  });
</script>

select.php:

<?php
require_once 'db.php';

if(!isset($_SESSION))session_start();

if(isset($_SESSION['user_id'])) {
  $user_id = $_SESSION['user_id'];
}

$status = 'unread';

    $sql = $conn->prepare("SELECT * FROM noti WHERE status = :status AND user_id = :user_id");
    $sql->bindParam(':user_id', $user_id, PDO::PARAM_INT);
    $sql->bindParam(':status', $status, PDO::PARAM_STR);
    $sql->execute();
    $countNT = $sql->rowCount();

    echo $countNT;

    $conn = null;

    ?>

update php.:

<?php
require_once 'db.php';

if(!isset($_SESSION))session_start();

if(isset($_SESSION['user_id'])) {
  $user_id = $_SESSION['user_id'];
}

$status = 'read';
$sql = $conn->prepare("UPDATE noti SET status = :status WHERE user_id = :user_id");
$sql->bindParam(':user_id', $user_id, PDO::PARAM_INT);
$sql->bindParam(':status', $status, PDO::PARAM_STR);
$sql->execute();
$countNT = $sql->rowCount();

echo $countNT;

$conn = null;
?>
  • I couldn’t understand exactly what you’re trying to do?

  • @Leandroangelo, you know those Facebook notifications? When a notification arrives it shows a number next to an icon, and when you click the icon the counter disappears. As you may notice, in my code, when I click on the icon I make an Ajax request, to execute the query that is in the file update.php, and this request doesn’t work more than once on the page, so I’m having to refresh the page.

  • @Leandroangelo I don’t know if you still don’t understand. But basically I want to run the query that is inside the file update.php as many times as necessary, without having to refresh the page.

  • apparently has nothing to restrict update.php from being called more than once... If you click the bell there will be a post for this script, what I don’t see is some instruction to display and hide the bell. And perhaps it would be interesting to rethink your strategy... to make a request per second to bring in the select.php can bring up many server and client-side performance issues.

  • @Leandroangelo The code to hide the bell is this: if($countNT >= 1){ echo $countNT; } I didn’t ask the question because it’s relevant. The performance problem I can solve later, you have some idea of why I’m having to refresh the page?

  • Can you add all your html? I couldn’t find the #datacount

  • @edsonalves I use echo to print this html, and forgot to put the <span> that has the id #datacount, sorry. I’ve already edited the code.

  • So that there is no doubt, the code in the question is all the code I use, I had only removed the if($countNT >= 1){ echo $countNT; }, that does not interfere with anything in the error I described.

  • I don’t understand the bell button. From what I understand, you can click the time you want on it that will call Ajax to update the BD without having to refresh the page. Now, you have a serious code problem using setInterval to call Ajax. This is probably creating a bottleneck on the server. Imagine you make requests every second without waiting for one to be finished and already make another one over after 1 second, and still make another Update request on top. The chance of this working is minimal (not to say null).

  • @Sam As I said, only updates the comic once without updating the page. You said I should not use senInterval, maybe that’s why I have this problem? If yes. what do you recommend?

  • @Sam I suspect that my explanation in question n was very clear. Pq is appearing commenting saying that (I got it right). Which part did you understand? I will try to edit the question.

  • @Sam On second thought, now that I’ve remembered something, I was running some tests, at the time mine senIntervel is 1000, I remembered that when I switched the 1000 by 10000, the database n would update neim even if I reloaded the page.

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.