0
I have a file called processa_vis.php where brings the result of all ID with online status, until then all right and usually displays users online on the screen where I call the function.
However I would like you to update the page but only the text where displays the number of users online. I basically include the.php process.php file with include and then call the $result variable where the number of online users is stored within the process.php, I wanted to update this result every 5 seconds.
I saw in some places use jQuery with setInterval but I have no experience with jQuery. It seems to be too simple but several failed attempts.
Archives:
parse_vis.php
<?php
include_once("conexao.php");
$query = "SELECT * FROM usuarios WHERE status_online = 'S';";
$sql = mysqli_query($conn, $query);
$result = mysqli_num_rows($sql);
echo $result;
?>
index php.
<?php
session_start();
include("conexoes/conexao.php");
if($_SESSION["id"]){
header("location: painel.php");
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title>Banco de Dados - Login</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#">Banco de Dados</a>
</div>
</nav>
<div class="container">
<h4 class="mt-4 display-4 text-center">Área de Login</h4><hr>
<div class="col-md-6 offset-md-3 mt-5">
<form method="post" action="conexoes/login.php" class="p-3" style="background-color: #e4e4e4; border-top: 20px #999999 solid;">
<?php
if(isset($_SESSION['msg'])){
echo "<div class='col-md-6 offset-md-3'>";
echo $_SESSION['msg'];
unset($_SESSION['msg']);
echo "</div>";
}
?>
<div class="form-group">
<div class="col-md-6 offset-md-3">
<label for="inputLogin">Login</label>
<input class="form-control" name="login" id="InputLogin" placeholder="Usuário">
</div>
</div>
<div class="form-group">
<div class="col-md-6 offset-md-3">
<label for="InputSenha">Senha</label>
<input type="password" class="form-control" name="senha" id="InputSenha" placeholder="Senha">
</div>
</div>
<div class="col-md-6 offset-md-3">
<button type="submit" id="btnLogin" class="btn btn-block btn-primary">Entrar</button>
</div><br>
<span>Ainda não possui uma conta?</span><br>
<small class="text-muted"><a href="cadastrar.php">Crie uma aqui.</a></small>
</form>
</div>
<div id="qtd_usuarios_online"></div>
<div class="footer" style="margin-top: 20px; margin-bottom: 10px;"><hr>
<footer>
<p class="text-muted text-center">© 2019</p>
</footer>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){ // iniciar assim que o documento estiver carregado
setInterval(update_visitas(),5000); // vai rodar a função a cada 5000 milésimos (5 segundos)
function update_visitas(){ //função que será carregada
$.ajax({url: "conexoes/processa_vis.php", // ajax que vai carregar o arquivo PHP
cache: false, // remover cache caso haja
success: function(result){ //se carregar com sucesso, carrega os dados do PHP
$('#qtd_usuarios_online').html(result); //exibe dentro da DIV #qtd_usuarios_online a saída do PHP
}});
}
});
</script>
</body>
</html>
Can you put some of those failed attempts to see where you’re struggling?
– Woss
I don’t have it here because I erased all attempts
– Carlos Pereira
But I modified and put the basic code in the files
– Carlos Pereira