Update data automatically without refreshing the page

Asked

Viewed 2,310 times

0

Maybe this doubt is quite "beast", but I’ve tried several ways and I’m not succeeding. I have a page that counts a quantity of products, but I need the data presented through the query that is on the page, be updated every 1 second without the page is updated. I tried some solutions in Ajax and JS, but I could not. :(

Does anyone have any tips on how to do this? Below is the php code I’m using:

$con = mysql_connect('localhost', 'root', 'password');
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
$user = mysql_real_escape_string($user);
$pwd = mysql_real_escape_string($pwd);

mysql_select_db("fabrica", $con);

$sql="SELECT COUNT(codigo) FROM inclusao_codigo WHERE cod_linha = '1' AND cod_turno = '1' AND data = CURRENT_DATE AND erro_ean IS NULL'";

$total = mysql_query($sql);


<link rel="stylesheet" type="text/css" href="../css/rhino/sc_inettuts.css" />
<link rel="stylesheet" type="text/css" href="../css/rhino/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="../css/rhino/gridstack.min.css" />
<link rel="stylesheet" type="text/css" href="../css/rhino/Sc9_Rhino_grid.css" />
<link rel="stylesheet" type="text/css" href="../css/rhino/Sc9_Rhino_gridLTR.css" />
<link rel="stylesheet" type="text/css" href="../css/rhino/Sc9_Rhino_container.css" />
<link rel="stylesheet" type="text/css" href="../css/rhino/Sc9_Rhino_containerLTR.css" />
<div style="display: inline-block">
<div class="widget-content widget-content-title-none scContainerIndexMoldura scContainerIndexMoldura_widget15" style="height: 100%" id="id-div-iframe-4">
<div style="text-align: center; padding-top: 15px">
   <div class="clearfix"><span style="font-size:20px; font-weight: bold;"><img src="../_lib/img/sys__NM__img__NM__certificate_full.png"><br>Total de Produtos</span></div>
   <div class="clearfix"><span style="float: right; font-size:15px;">Linha 1</span></div>
   <div class="clearfix"><span style="font-size:70px;"><?php echo $total; ?></span></div>
</div>

1 answer

0


meu_script.js

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function(){
        setInterval(function(){
            $("#count").load('meu_script_2.php')
        }, 1000);
    });
</script>

meu_script_2.php

$con = mysql_connect('localhost', 'root', 'password');
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
$user = mysql_real_escape_string($user);
$pwd = mysql_real_escape_string($pwd);

mysql_select_db("fabrica", $con);

$sql="SELECT COUNT(codigo) FROM inclusao_codigo WHERE cod_linha = '1' AND cod_turno = '1' AND data = CURRENT_DATE AND erro_ean IS NULL'";

$total = mysql_query($sql);
printf("%d",$total);

Browser other questions tagged

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