如何防止PHP中的多个表单提交

时间:2016-07-23 09:28:24

标签: javascript php jquery

我尝试停止多个提交按钮。我是php和javascript的新手。我确实尝试了很多不同的堆栈溢出答案选项。我确实检查了会话令牌,但它对我不起作用,因为我从JavaScript提交表单。您可以看到禁用代码按钮。当我点击按钮它被禁用并形成提交但它没有达到btn付费的PHP代码。请帮忙。

php code1:

<?php
include('sessionstart.php');
include('session.php');
include('processtransaction.php');
?>
<script src="js/mytransaction.js"></script>

php code1将调用javascript并且javascript具有表单提交。如果表单已提交,则禁用/(会话令牌处理)已支付的按钮,它应该调用btn-paid的代码。

javascript代码mytransaction:

$(document).ready(function() {
    var table  = $('#myTransactionitems').dataTable();  //Initialize the datatable
    var user = $(this).attr('id');
    if(user != '') 
    { 
        $.ajax({
            url: 'transactions',
            dataType: 'json',
            success: function(s){
                console.log(s);
                table.fnClearTable();
                for(var i = 0; i < s.length; i++) {
                    var disp1 = '';
                    if (s[i][4] != 'Reserved') {
                        disp1 = 'display:none;'
                    }
                    table.fnAddData([
                        "<form method='post' action='reservationdetails'><input name = 'transactionid' type='hidden'\
                    value='"+s[i][0]+"'></input><input type='submit' id = 'btn-bank' name='btn-bank' value = '"+s[i][0]+"' class='btn btn-link'>\
                   </input></form>",
                        s[i][1],
                        s[i][2],
                        s[i][3],
                        s[i][4],
                        s[i][5],
                        "<form method='post'><input name = 'transactionid' type='hidden'\
                    value='"+s[i][0]+"'><input name = 'donationid' type='hidden'\
                    value='"+s[i][2]+"'><input name = 'splitamount' type='hidden'\
                    value='"+s[i][3]+"'></input></input><input type='submit' id = 'btn-paid' name='btn-paid' value = 'Paid' onclick='this.disabled=true;this.form.submit();'  style='" + disp1 +"' class='btn btn-sm btn-success pull-left '>\
                   </input></form><form method='post'><input name = 'transactionid' type='hidden'\
                    value='"+s[i][0]+"'><input name = 'donationid' type='hidden' \
                    value='"+s[i][2]+"'><input name = 'splitamount' type='hidden'\
                    value='"+s[i][3]+"'></input><input type='submit' id = 'btn-cancel' name='btn-cancel' value = 'Cancel' onclick='this.disabled=true;this.form.submit();' style='" + disp1 +"' class='btn btn-sm btn-danger pull-right'>\
                   </input></form>"
                        ]);                             
                } // End For

            },
            error: function(e){
                console.log(e.responseText);    
            }
        });
    }   
});

php code2 processtransaction:

<?php
 if (isset($_POST['btn-paid'])) {
    require_once("dbcontroller.php");
    $db_handle = new DBController();
    $conn = $db_handle->connectDB();

    $query = "update MYTRANSACTION set STATUS =? where DONATION_ID=? AND ID=?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("sii",$status,$donation_id, $transaction_id);
    $stmt->execute();
    $stmt->close();

    $db_handle->closeDB($conn);
}
?>

1 个答案:

答案 0 :(得分:1)

如果你想阻止用户在页面加载之前点击两次(导致2个帖子),最好使用javascript。由于您已经在使用jQuery,因此可以执行以下操作:

$("input[type='submit']").click(function(e) {
    e.preventDefault(); // Prevent the page from submitting on click.
    $(this).attr('disabled', true); // Disable this input.
    $(this).parent("form").submit(); // Submit the form it is in.
});

编辑:

为了满足在页面加载之前立即提交的人(即在加载JS之前),您可以进行以下更改。

在HTML表单中,将“禁用”添加到“提交”按钮:

<input type="submit" value="Submit" disabled />

将您的JS更改为以下内容:

$("input[type='submit']").attr('disabled', false); // Enable this input.
$("input[type='submit']").click(function(e) {
    e.preventDefault(); // Prevent the page from submitting on click.
    $(this).attr('disabled', true); // Disable this input.
    $(this).parent("form").submit(); // Submit the form it is in.
});

这样做会使提交按钮被禁用,直到JS加载完毕。一旦JS加载,它将启用提交按钮,然后剩余的东西将像以前一样工作。