当用户在php中单击时,尝试使用javascript函数运行mysql查询特定时间

当用户在php中单击时,尝试使用javascript函数运行mysql查询特定时间

问题描述:

When user click claim button, i want to run javascript function every 5 sec. After i build up this code, and clicked the button but the script doesn't work. I'm not good at javascript at all. All i know is some php and mysql.

When user clicked claim button, the function send() should call payout() function and run this crul() function which is inside payout() function. I don't know where i make mistake as i don't know much javascript.

this is the button function i build up. don't know if it wrong or not.

<button onclick="send()" class="btn-success"><i class="fas fa-exclamation-circle"></i> Claim <i class="fas fa-exclamation-circle"></i></button>
<script>
function send() {
   setInterval(function(){ payout(); }, 5000);
}
</script>

This is the function payout()

function payout(){
    $sql = $conn->query("UPDATE wallets SET balance = balance - 1 WHERE wallet = '$wallet' " );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://faucethub.io/api/v1/send");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
            "api_key=$apikey&to=$wallet&currency=$shortcurrency&amount=$doge ");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec ($ch);
    curl_close ($ch);
}

The result should be payout() function should send to that $wallet every 5 sec

First, you need to create one PHP file and move your PHP payout() in php file. second, you need to ajax call in the javascript interval.

payout.php

//add the following code after your connection object
function payout(){
    $sql = $conn->query("UPDATE wallets SET balance = balance - 1 WHERE wallet = '$wallet' " );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://faucethub.io/api/v1/send");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
            "api_key=$apikey&to=$wallet&currency=$shortcurrency&amount=$doge ");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec ($ch);
    curl_close ($ch);
}

index.html

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="send()" class="btn-success"><i class="fas fa-exclamation-circle"></i> Claim <i class="fas fa-exclamation-circle"></i></button>
<script>
function send() {
    setInterval(function () {
        $.ajax({
            type: 'POST',
            url: 'payout.php',
            success: function (data) {
                console.log("payout attempt");
            },
        });
    }, 3000);
}
</script>

</div>