Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
Ajax inserting duplicate records in Mysql Database

I have the following Ajax Code for submitting data to Mysql DB.

<script>
var amountdue;
var amount;
$('#btn-submit').on('click',function(e){
  e.preventDefault();
  if ($("#customer").validationEngine('validate')) {
    swal({
      title: "Submit Confirmation",
      text: "Are you sure to submit the data?",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#126495",
      confirmButtonText: "Submit",
      closeOnConfirm: false
    }, 
    function(isConfirm){
      if (isConfirm) {
        amountdue =  "<?php echo $amount_due;?>";
        amount = "<?php echo $amount;?>";
        $.ajax({
          type: "POST",
          url: "submit.php",
          cache: false,
          dataType : "text",
          data: {amountdue : amountdue, amount : amount},
          success: function(data) {
            window.location.href = 'customers';
          },
          error: function(result) {
            swal({
              title: "Error",
              type: "warning",
              text: "Sorry, the data could not be updated due to some reasons.",
              confirmButtonColor: "#126495"
            });
          }
        });
      } //confirm
    });
  }
});
</script>

The issue is that sometimes (Not always) data is getting inserted 2 and more times in mysql DB..How can i prevent this behavior on Ajax Submit ??

will $('#btn-submit').unbind();

work on success ?? Requesting help...

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try this:

As the first line of your click event handler, do

e.disabled = true;

Then, in both your success and error handlers for AJAX, do this

e.disabled = false;

The point of this is to suppress unintentional double clicks on your button until the AJAX operation finishes.

over 4 years ago · Santiago Trujillo Report

0

$('#btn-submit').off().on('click',function(e){
 ……..Your code...

Try like this.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!