1

I have a url that I can call with a userid, and it will update an SQL table. (e.g. domain.com/postback.php?userid=userid). That works fine manually, but not when trying to get JavaScript to call it. I've only just started learning JavaScript as of 3 days ago, so please forgive me if it's an easy one - but I can't see it.

I call the JS in question here:

<input type='button' id='countdw' value='Wait 30s'  class='btn btngo disabled'>
<script>
    var secsLeft = 30;
    setInterval(function(){
        secsLeft--;
        if(secsLeft > 0){
            $('#countdw').val('Wait ' + secsLeft + 's');
        } else if(secsLeft == 0){
            $('#countdw').removeClass('disabled');
            $('#countdw').val('Go');
            $('#countdw').attr("onclick","doSomething2()"); 
        }
    }, 1000);

Here is my JS. I've tried many different ways and this is just one of them.

<script type="text/javascript">
$(document).ready(function(){
    function doSomething2(){
        $.ajax
            { url: 'update.php',
                data: { userid : userid },
                type : 'GET',
                dataType: 'json',
                success : function ( jsXHR) {
                },
                failed : function(status,data){ }
               }
         );
    }

});
</script>

<?php
$subid = $_GET['userid'];
?>

Here is the update.php script (I'm aware it's not mySql - yes I need a new book.) I just can't get the JS above to call it.

$uped = 1;
$subid   = $_REQUEST['userid'];
mysql_query("UPDATE ".MYSQLTABLE." SET view=view+".$uped." WHERE userid='".$subid."'") or die(mysql_error());

 mysql_close();

 ?>
Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

This seems to be working for me.

    function count() {
        window.secsLeft--;
        if (window.secsLeft > 0) {
            $('#countdw').val('Wait ' + window.secsLeft + 's');
        } else if (window.secsLeft === 0) {
            $('#countdw').removeClass('disabled');
            $('#countdw').val('Go');
            $('#countdw').attr("onclick", "doSomething2()");
        }
    }


    function doSomething2() {
            $.ajax({
                url: 'update.php',
                data: {
                    userid: <?php echo $userid; ?>
                },
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                },
                error: function (data) {
                    console.log(data);
                }
            });
        }
    
    (function () {
        "use strict";
        window.secsLeft = 30;

        setInterval('count()', 1000);
    }());
<input type="button" id="countdw" value='Wait 30s' class="btn btngo disabled">
kneeki
  • 2,444
  • 4
  • 17
  • 27