How to update the PDO database after running a Javascript countdown function

0

I'm doing a countdown Javascript, and I would like it when the count is == 0 , run a update in the database where it would go 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 am currently using this code and it is 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'];
?>
    
asked by anonymous 18.02.2017 / 22:32

1 answer

0

The if (count == 0) because it must be outside the if ((count -1) >=0) :

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(){
            }
        });
    }
    setTimeout('start();', 1000);
}

start();
    
20.02.2017 / 20:42