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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码