dougai8673 2019-05-30 06:28 采纳率: 100%
浏览 91
已采纳

当用户在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

  • 写回答

1条回答 默认 最新

  • dongsuichi6529 2019-05-30 06:36
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新