1
I have a column in the database called time
, she owns an event that does the following:
UPDATE users SET time = time - 1
As default gets 1800 seconds (30 minutes) right?
Now I wish recover the time going down I tried the following:
<?php
$database = new DB;
/*Selectiono meu usuário*/
$select = $database->select($username);
/*Aqui retorno tudo*/
$fetch = $select->fetch(PDO::FETCH_ASSOC);
/*Aqui imprimo na tela o conteúdo do campo time*/
echo $fetch['time'];
?>
Now I tried to do the following, to recover, but it does not appear. I mean, it does not work.
$(document).ready(function() {
$('.contagem').css('display', 'block');
$('.contagem').html("<?php echo $fetch['time']?>");
});
Error in first line:
$(document).ready(function() {
I reviewed this site and nothing helped me.
Edit
Whole code
<?php
$database = new DB;
/*Selectiono meu usuário*/
$select = $database->select($username);
/*Aqui retorno tudo*/
$fetch = $select->fetch(PDO::FETCH_ASSOC);
/*Aqui imprimo na tela o conteúdo do campo time*/
echo $fetch['time'];
?>
<!DOCTYPE html>
<html>
<head>
<title>
Contagem
</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<span class="contagem" style="display: none;"></span>
<script type="text/javascript">
$(document).ready(function() {
$('.contagem').css('display', 'block');
$('.contagem').html("<?php echo $fetch['time']?>");
});
</script>
</body>
</html>
Note: When arriving at 0 Reload a page.
Is this jQuery code in the PHP file or in a separate Javascript file? It needs to be in the PHP file for the PHP code snippet to be interpreted. If you are in the JS file, PHP will never see this chunk of code, although the effect I see from this would be to display the PHP code in the element
.contagem
instead of the value itself. You can put your complete code and error message?– Woss
Dude, make an ajax request, I particularly hate this kind of approach that mixes the 2.
– gabrielfalieri
Matheus, not read() but ready()
– Bsalvo
If you are applying these styles as soon as the page is loaded you can do this directly in the document’s HTML without needing any script. Help your browser :)
– Bsalvo
@Andersoncarloswoss, is in the same file, the whole code is the same, has nothing else. of course opens and closes tags
PHP
andscript
clear-cut.– Matheus Borjes
Good but I wanted count in real time, countdown understood?
– Matheus Borjes
If it is in real time, the easiest is to count only with Javascript, using the function
setTimeOut
orsetInterval
.– Woss