2

I'm noticing ajax being called several times resulting in duplicate entries in mySQL. What's the deal? Can anyone see anything in my code?

$("#form-addcomment").live('submit', function(e) {
    e.preventDefault();

    var values = $(this).serializeArray();
    values = $.param(values);

    $.ajax({
        type: "POST", 
        url: "/components/m/actions/index.php", 
        data: "command=comments_add&" + values,
        dataType: "html",
        success: function(data){
            alert(data);
            return false;
        }
    });

    return false;
});
Gajotres
  • 57,309
  • 16
  • 102
  • 130
Mike
  • 980
  • 3
  • 15
  • 29

1 Answers1

2

You are probably having problems with multiple event binding, this should fix your problem:

$(document).off('submit', "#form-addcomment").on('submit', "#form-addcomment" ,function(e) {
    e.preventDefault();

    var values = $(this).serializeArray();
    values = $.param(values);

    $.ajax({
        type: "POST", 
        url: "/components/m/actions/index.php", 
        data: "command=comments_add&" + values,
        dataType: "html",
        success: function(data){
            alert(data);
            return false;
        }
    });

    return false;
});

There are also other ways of multiple event binding, to find out more take a look at my other article and search for chapter called Prevent multiple event binding/triggering: https://stackoverflow.com/a/14469041/1848600

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130