【问题标题】:Ajax inserting duplicate records in Mysql DatabaseAjax在Mysql数据库中插入重复记录
【发布时间】:2020-11-28 13:54:28
【问题描述】:

我有以下用于向 Mysql DB 提交数据的 Ajax 代码。

<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>

问题是 有时(并非总是) 数据会在 mysql DB 中插入 2 次或更多次。如何防止 Ajax Submit 出现这种行为??

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

为成功而努力??请求帮助...

【问题讨论】:

  • 能否缩进你的代码,这样引用很难阅读
  • 缩进代码...
  • 这里没有表示双重触发,可以在 submit.php 中完成
  • 是的,我不知道在哪里或为什么会重复提交,除非 swal 正在做自己的事情,导致它不止一次地点击自己的回调。
  • 不确定sweetalert 是否是罪魁祸首。再次解除约束和绑定会有所帮助吗??成功:函数(数据){ $('#btn-submit').unbind(); $('#btn-submit').bind('click'); window.location.href = '客户'; },

标签: php jquery mysql ajax


【解决方案1】:

试试这个:

作为点击事件处理程序的第一行,做

e.disabled = true;

然后,在 AJAX 的成功和错误处理程序中,执行此操作

e.disabled = false;

这样做的目的是在 AJAX 操作完成之前禁止无意中双击您的按钮。

【讨论】:

    【解决方案2】:
    $('#btn-submit').off().on('click',function(e){
     ……..Your code...
    

    像这样试试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2023-03-19
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      相关资源
      最近更新 更多