Home > Mobile >  Refresh page when value is = 1
Refresh page when value is = 1

Time:11-17

Hello im trying make a system for when on value on my datebase is 1 my index page refresh. this is my code! All PHP code works, only my index.php not refresh.

index.php

<script>
  inverval_timer = setInterval(function update() { 
    $.get("base_de_dados/update/loadMensagens.php", function(data) {
      $("#numseiCategoria").html(data);
      window.setTimeout(update);
    })
  }, 5000);
</script>

loadMensagens.php

<?php
include_once '../bdados.php';
$fila = $conn->query("SELECT * FROM tickets_update");

while($Filas = $fila->fetch_assoc()){
  $condicao = $Filas['condicao'];
}

if($condicao == 1){
  $condicao = 0;
  $queryAtualizar = $conn->query("UPDATE tickets_update SET condicao='$condicao'");
  echo "
  <div id='a'></div>
  <script>
    $.ajax({url:'updateMensagens.php', success:function(result){
      $('#a').html(result)
    }});
  </script>";
}

?>

updateMensagens.php

<script>
    $(document).ready(function(){
        location.reload();
    });
</script>

CodePudding user response:

At a glance there are a couple of reasons your page isn't reloading. Firstly, the reload method being wrapped in that .ready() probably prevents it from being called, as I believe the ready event only fires when the DOM first loads.

$(document).ready(function(){
    location.reload(); // Will never fire if script is added to DOM after initial load.
});

But I think there's another issue as your code simply appends this HTML...

<div id='a'></div>
<script>
  $.ajax({url:'updateMensagens.php', success:function(result){
    $('#a').html(result)
  }});
</script>";

...onto the end of #numseiCategoria's inner text, which probably doesn't make the browser execute the script anyway (I'm assuming that jQuery's .html() is basically an alias for innerHTML here, I can't be bothered to go and check).

But, in terms of good practices, there's more to it than that...

updateMensagens.php seems to be incredibly redundant, unless there's more to it than you're showing. Let's have a think about how you intended it to work, ignoring the fact that your method of adding scripts to the page is incorrect.

You have your main script in index.php, which sends a get request to loadMensagens.php, which does some database stuff. So far so good... Your PHP script then echoes some JS, which your main script appends to the page. This JS tells the client to send another get request, this time to updateMensagens.php, and to once again append the result to the page. This second request returns only a script telling the browser to reload the page. And now we've run into problems.

This is a really awkward and long-winded way to go about this, especially once you try to scale the approach up to larger projects. You're trying to do certain things with PHP which are much more easily done with JS. I'll briefly highlight a couple of things for you.

Firstly, echoing HTML back to the client like that is not great, it gets very unwieldy very quickly. It's much cleaner to return any necessary data to the front end as JSON (or a similar format) and handle generating HTML with JS. jQuery makes generating complex documents rather easy, as you're already using it I'd recommend that approach.

Secondly, this system of using ajax requests to fetch a script from the server to append to the page to perform a simple action with JavaScript is diabolical. Please see my untested, 4AM alternative.

index.php

<script>
  inverval_timer = setInterval(function update() { 
    $.get("base_de_dados/update/loadMensagens.php", function(data) {
      let res = JSON.parse(data);
      if(res.condiciao === true) {
        location.reload();
      }
    });
  }, 5000);
</script>

loadMensagens.php

<?php
$return = [];
include_once '../bdados.php';
$fila = $conn->query("SELECT * FROM tickets_update");

while($Filas = $fila->fetch_assoc()){
  $condicao = $Filas['condicao'];
}

if($condicao == 1){
  $return['condicao'] = true;
  $condicao = 0;
  $queryAtualizar = $conn->query("UPDATE tickets_update SET 
condicao='$condicao'");
  echo json_encode($return);
} else {
  $return['condicao'] = false;
  echo json_encode($return);
}

As an addendum, you seem to be using setTimeout wrong. On one hand I'm quite sure the method is supposed to take 2 arguments, but on the other hand I'm not sure why it's being used at all.

Goodnight.

  • Related