0
I’m making a Countdown Javascript, and would like when the count was == 0, execute a update in the database where would add a value
<script type="text/javascript">
var count=new Number();
var count=<?php echo $time ?>; //Valor da variável do banco de dados
function start(){
if ((count -1) >=0) {
count=count - 1;
if (count == 0)
{
// Quando count == 0 Adiciona(UPDATE) valor +10 dentro de uma variável do banco de dados
}
tempo.innerText=count;
setTimeout('start();', 1000);
}
}
</script>
EDIT ##
I’m using this code right now and it’s not working
HTML
<body onload="start();">
<div id="tempo"></div>
<form method="POST" action="insert.php" id="my_form">
<input type="text" name="id_user" value="ID_Que_Eu_Quero_Passar" style="display: none;">
<input type="text" name="id_view" value="ID_Que_Eu_Quero_Passar2" style="display: none;">
</form>
</body>
JQUERY AJAX
var count=new Number();
var count=10;
function start(){
if ((count -1) >=0) {
count=count - 1;
if (count == 0) {
$.ajax({
url: $(this).attr('insert.php')
,async: true
,cache: false
,type: $(this).attr('POST')
,data: $(this).serialize()
,dataType: 'json'
,success: function(data){
console.log("Update aconteceu");
if(data.success == 'yes'){
alert('Gol!');
}
else{
alert('insert failed!');
}
}
,error: function(){
}
,complete: function(){
}}); }
tempo.innerText=count;
setTimeout('start();', 1000);}}
PHP
<?php
echo $_POST['ID_Que_Eu_Quero_Passar'];
echo $_POST['ID_Que_Eu_Quero_Passar'];
?>
Within the
if (count == 0)
you can make an AJAX request for a PHP script that does INSERT in the database.– Marcos
@Marcos I’m trying this but so far without success, I’m not able to make ajax perform the update
– Charlie Sheen
Ask the question of the AJAX and PHP code you are using.
– Marcos
@Mark ready friend, I edited and put everything there
– Charlie Sheen
If you make a
var_dump
from the $_POST, which comes?– Marcos
@Marcos appears indefinite value of $_POST, but the problem is there inside the
if count {}
I think, why is the counter locking on 1, and does not perform the ajax– Charlie Sheen