Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

Try like this.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda