-1

I'm trying stop multiple submit buttons.I'm new to php and javascript.I did try lot of different options of stack overflow answers.I did check session token but it is not working for me because i'm submitting the form from JavaScript.You can see the code to disabled button . When i click the button it is disabled and form submited but it is not reaching the btn-paid code of php. Please help.

php code1:

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

php code1 will call the javascript and javascript has the form submit. If the form is submitted then it disable/(session token process) the button paid and it should call the code of btn-paid.

javascript code 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);
}
?>
DanMan
  • 11,323
  • 4
  • 40
  • 61
Santosh Reddy
  • 57
  • 1
  • 9
  • it's better to do such a thing in javascript, because doing it PHP means the second and subsequent posts have already been performed – Jaromanda X Jul 23 '16 at 09:30
  • `php code1 will call the javascript` - no it wont. PHP is server side code, javascript is client side, there is no way for PHP to call javascript – Jaromanda X Jul 23 '16 at 09:36
  • jaromanda can you please point me which part of the code i'm doing wrong. – Santosh Reddy Jul 23 '16 at 09:38
  • jaromanda sorry it is mistake in typing php code1 will include the jquery. – Santosh Reddy Jul 23 '16 at 09:45
  • I was pointing out your assertion that PHP calls javascript was an incorrect assertion, nothing about the code itself, as I never read javascript code that includes html in quotes, it just makes my inner child cry when I see that – Jaromanda X Jul 23 '16 at 09:50

1 Answers1

1

If you want to prevent the user from clicking twice before page ends loading (causing 2 posts), it would better to use javascript. As you are already using jQuery, you can do the following:

$("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.
});

Edit:

To cater for someone immediately submitting before page loads completely (ie before JS loads), you can make the following changes.

In your HTML form, add disabled to your submit button:

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

And change your JS to the following:

$("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.
});

What this does is makes the submit button disabled until JS has loaded. Once JS loads, it will enable the submit button and then remaining things will work as before.

Muhammad Ali
  • 668
  • 1
  • 9
  • 24
  • Thanks AliIshaq.I used your code below jquery inside mytransaction.It works for me. – Santosh Reddy Jul 23 '16 at 09:53
  • Great! Don't forget to mark the answer as correct. You can also run the same functions on the Enter key, take a look here for it: http://stackoverflow.com/questions/979662/how-to-detect-pressing-enter-on-keyboard-using-jquery – Muhammad Ali Jul 23 '16 at 10:19
  • AliIshaq this code works in my local machine but if i put the code in godaddy and if the click the paid or cancel button immediatly in fraction of seconds then it takes 7 to 8 clicks then it is disabling.Sorry for asking this again.Do u have any idea please. – Santosh Reddy Jul 23 '16 at 20:40
  • The above code is not working if you submit the button multiple times immediately within fraction of seconds. – Santosh Reddy Jul 23 '16 at 22:48
  • I have edited my answer to add a workaround to fix that. – Muhammad Ali Jul 24 '16 at 06:13
  • your code is always disabled even after js loading and even if i click.The problem is after javascript loading when user clicks mutiple times in fraction of sec- @AliIshaq – Santosh Reddy Jul 24 '16 at 12:11
  • Have you removed `onClick` from your input element? It could be conflicting with our jQuery code. – Muhammad Ali Jul 24 '16 at 12:50
  • yes i removed it but if you click multiple times in fraction of seconds this happens.This click may before it is calling disabled method.If you click once and 1 sec later it will be disabled.That part works. I'm talking about funny users :) – Santosh Reddy Jul 24 '16 at 14:02