Javascript AJAX在数据库中输入数据两次

Javascript AJAX在数据库中输入数据两次,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个功能,用户可以报告一篇文章,用户单击报告按钮,然后立即输入报告信息。这是可行的,问题是如果页面没有重新加载,并且用户决定报告第二次发布,那么该报告的数据将进入数据库两次。为什么会这样 代码如下: $(document).ready(function() { $(".report_post").click(function(e) { var nid = $(this).attr("id"); $("#report_reason").dialog({ resi

我有一个功能,用户可以报告一篇文章,用户单击报告按钮,然后立即输入报告信息。这是可行的,问题是如果页面没有重新加载,并且用户决定报告第二次发布,那么该报告的数据将进入数据库两次。为什么会这样

代码如下:

$(document).ready(function() {
  $(".report_post").click(function(e) {
    var nid = $(this).attr("id");
    $("#report_reason").dialog({
      resizable: false,
      height: 300,
      width: 400,
      modal: true,
    });

    $('.submit_report_post').click(function() {
      var content = $("#report_content").val();
      var type = "Post";
      if ($('input[name="report"]:checked').length > 0 && (content != null &&
        content != "")) {
        var complaint = document.querySelector('input[name="report"]:checked').value;
        alert('Reported!');
        $.ajax({
          type: 'POST',
          url: 'php/report_post.php',
          data: {
            type: type,
            nid: nid,
            reason: complaint,
            content: content,
          },
          cache: false,
          success: function(data) {
            $("#report_content").val("");
            $("input[name='report']").prop('checked', false);
            //$("#report_reason").dialog('close');
          }
        });
      } else {
        alert('Fill all of the information!');
      }
    });
    e.preventDefault();
  });
});

您正在绑定
单击
$('.submit_report_post')
每次单击
$(“.report_post”)
时,您都需要在第一次绑定之外进行绑定

$(document).ready(function() {
  $(".report_post").click(function(e) {
    var nid = $(this).attr("id");
    $("#report_reason").dialog({
      resizable: false,
      height: 300,
      width: 400,
      modal: true,
    });
    e.preventDefault();
  });

  $('.submit_report_post').click(function() {
    var content = $("#report_content").val();
    var type = "Post";
    if ($('input[name="report"]:checked').length > 0 && (content != null &&
      content != "")) {
      var complaint = document.querySelector('input[name="report"]:checked').value;
      alert('Reported!');
      $.ajax({
        type: 'POST',
        url: 'php/report_post.php',
        data: {
          type: type,
          nid: nid,
          reason: complaint,
          content: content,
        },
        cache: false,
        success: function(data) {
          $("#report_content").val("");
          $("input[name='report']").prop('checked', false);
          //$("#report_reason").dialog('close');
        }
      });
    } else {
      alert('Fill all of the information!');
    }
  });

});

您正在绑定
单击
$('.submit_report_post')
每次单击
$(“.report_post”)
时,您都需要在第一次绑定之外进行绑定

$(document).ready(function() {
  $(".report_post").click(function(e) {
    var nid = $(this).attr("id");
    $("#report_reason").dialog({
      resizable: false,
      height: 300,
      width: 400,
      modal: true,
    });
    e.preventDefault();
  });

  $('.submit_report_post').click(function() {
    var content = $("#report_content").val();
    var type = "Post";
    if ($('input[name="report"]:checked').length > 0 && (content != null &&
      content != "")) {
      var complaint = document.querySelector('input[name="report"]:checked').value;
      alert('Reported!');
      $.ajax({
        type: 'POST',
        url: 'php/report_post.php',
        data: {
          type: type,
          nid: nid,
          reason: complaint,
          content: content,
        },
        cache: false,
        success: function(data) {
          $("#report_content").val("");
          $("input[name='report']").prop('checked', false);
          //$("#report_reason").dialog('close');
        }
      });
    } else {
      alert('Fill all of the information!');
    }
  });

});

您需要提交两次表单,一次是以普通方式提交,一次是通过AJAX提交。你有
e.preventDefault()
在您的代码中,它通常会停止典型的非AJAX提交,但是您从未创建
e
参数

更改:

$('.submit_report_post').click(function() {


这将使表单只通过AJAX代码提交。

您提交表单两次,一次是通过普通方式提交,一次是通过AJAX提交。你有
e.preventDefault()
在您的代码中,它通常会停止典型的非AJAX提交,但是您从未创建
e
参数

更改:

$('.submit_report_post').click(function() {


这将使表单只能通过AJAX代码提交。

您可以做很多事情。创建id为button的数组。单击按钮时,在数组中插入id。在发送ajax请求之前,检查值是否已经在数组中。希望你能做更多的事情。创建id为button的数组。单击按钮时,在数组中插入id。在发送ajax请求之前,检查值是否已经在数组中。希望很清楚这似乎是解决方案,我只是想知道如何传递
varnid=$(this.attr(“id”)到ajax脚本?我尝试在
submit\u report\u post
中创建一个单独的
var-nid
,它成功了。不过,我想知道如何传递
var-nid
。感谢这个解决方案:)这似乎就是解决方案,我只是想知道如何传递
varnid=$(this.attr(“id”)到ajax脚本?我尝试在
submit\u report\u post
中创建一个单独的
var-nid
,它成功了。不过,我想知道如何传递
var-nid
。感谢您提供此解决方案:)