How to update the PDO database after running a Javascript (Countdown) function

Asked

Viewed 77 times

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 I’m trying this but so far without success, I’m not able to make ajax perform the update

  • Ask the question of the AJAX and PHP code you are using.

  • @Mark ready friend, I edited and put everything there

  • If you make a var_dump from the $_POST, which comes?

  • @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

Show 1 more comment

1 answer

0

The if (count == 0) because he must be out of 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();

Browser other questions tagged

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