JQuery Submission Form Twice

I know that this question was answered several hundred times, but I was faced with a load of potential solitons, but none of them seem to work in my case.

Below is my form and code for submitting the form. It runs on a PHP script. Now I know that the script itself is not the reason for submitting, since I tried the form manually, and it only submits once.

The first part of jQuery code is related to opening a lightbox and pulling values ​​from the table below, I turned it on if for any reason this is a potential problem.

JQuery Code:

$(document).ready(function(){
    $('.form_error').hide();
    $('a.launch-1').click(function() { 
        var launcher = $(this).attr('id'),
            launcher = launcher.split('_');
        launcher, launcher[1], $('td .'+launcher[1]);
        $('.'+launcher[1]).each(function(){
            var field = $(this).attr('data-name'),
                fieldValue = $(this).html();
            if(field === 'InvoiceID'){
                $("#previouspaymentsload").load("functions/invoicing_payments_received.php?invoice="+fieldValue, null, function() {
                    $("#previouspaymentsloader").hide();
                });
            } else if(field === 'InvoiceNumber'){
                $("#addinvoicenum").html(fieldValue);
            }
            $('#'+field).val(fieldValue);
        });
    });
    $("#addpayment_submit").click(function(event) {         
        $('.form_error').hide();  
        var amount = $("input#amount").val();  
        if (amount == "") {  
            $("#amount_error").show();  
            $("input#amount").focus();  
            return false;
        }
        date = $("input#date").val();  
        if (date == "") {  
            $("#date_error").show();  
            $("input#date").focus(); 
            return false;
        } 
        credit = $("input#credit").val(); 
        invoiceID = $("input#InvoiceID").val(); 
        by = $("input#by").val(); 
        dataString = 'amount='+ amount + '&date=' + date + '&credit=' + credit + '&InvoiceID=' + invoiceID + '&by=' + by;
        $.ajax({
            type: "POST",
            url: "functions/invoicing_payments_make.php",
            data: dataString,
            success: function(result) {
                if(result == 1){                    
                    $('#payment_window_message_success').fadeIn(300);
                    $('#payment_window_message_success').delay(5000).fadeOut(700);
                    return false;
                } else {
                    $('#payment_window_message_error_mes').html(result);
                    $('#payment_window_message_error').fadeIn(300);
                    $('#payment_window_message_error').delay(5000).fadeOut(700);
                    return false;
                }
            },
            error: function() {
                $('#payment_window_message_error_mes').html("An error occured, form was not submitted");
                $('#payment_window_message_error').fadeIn(300);
                $('#payment_window_message_error').delay(5000).fadeOut(700);
            }
        });
        return false;
    });
});

Here is the html form:

<div id="makepayment_form">
  <form name="payment" id="payment" class="halfboxform">
    <input type="hidden" name="InvoiceID" id="InvoiceID" />
    <input type="hidden" name="by" id="by" value="<?php echo $userInfo_ID; ?>" />
    <fieldset>
      <label for="amount" class="label">Amount:</label>
      <input type="text" id="amount" name="amount" value="0.00" />
      <p class="form_error clearb red input" id="amount_error">This field is required.</p> 
      <label for="credit" class="label">Credit:</label>
      <input type="text" id="credit" name="credit" />
      <label for="amount" class="label">Date:</label>
      <input type="text" id="date" name="date" />
      <p class="form_error clearb red input" id="date_error">This field is required.</p>
    </fieldset>
    <input type="submit" class="submit" value="Add Payment" id="addpayment_submit">
  </form>
</div>

Hope someone can help as it drives me crazy. Thank.

+5
source share
6 answers

, . , event.stopImmediatePropagation() event.preventDefault().

:

$("#addpayment_submit").on('submit', function(event) {
  event.preventDefault();
  event.stopImmediatePropagation();
});
+30

, 'click'. , 'enter' ? .

:

$("#addpayment_submit").click(function(event) {
    event.preventDefault();
    //code
}

:

$("#payment").bind('submit', function(event) {
    event.preventDefault();
    //code
}

, , , .

+6

.

event.preventDefault(); 
event.stopImmediatePropagation();

.

+1

preventDefault() .

$("#addpayment_submit").click(function(event) {
    event.preventDefault();
    // Code here
    return false;
});

. preventDeafult() . , , ajax.

return false; (. ).

0

AJAX , form <input type="submit" .../> ( ). , . , event.stopPropagation().

0

, script @user2247104, , , .

However, @Cumbo, @Barmar were also on the right track, except to make it work

event.preventDefault();

You need to place at the bottom of the script:

});
event.preventDefault();
return false;

Thank you all for your help :)

0
source

All Articles