Problem with JS/Jquery/Ajax

Asked

Viewed 103 times

2

I have a button that when clicked it performs a function:

function searchTradeItems() {
    var uid = $("#useruniqid").val();
    var iusername = $("#username").html();
    var userid = $("#balmung-id").val();
    var itemType = $("#trade-type option:selected").val();
    var valueType = $("#trade-gold-type option:selected").val();
    if (valueType == 5) {
        $("#alert-box").show();
        $("#mensagem-alert").html("Insira o valor da qualidade entre 0 e 100");
        $("#alert-ok").click(function() {
            var iquality = $("#alert-value").val();
            $("#alert-box").fadeOut(200);
            $.ajax({
                url: "systems/action-trade.php",
                type: "POST",
                data: {
                    uid: uid,
                    iusername: iusername,
                    itemType: itemType,
                    valueType: valueType,
                    userid: userid,
                    iname: iname,
                    iquality: iquality
                },
                beforeSend: function() {},
                success: function(result) {
                    $("#user-painel-2").html(result);
                    $("#alert-value").val("");
                }
            });
        });
        $("#alert-cancel").click(function() {
            $("#alert-box").fadeOut(200);
        });
    };
    //----------------------------------------
}

Until then ok, it takes the typed data and sends with ajax, works, returns the result I want perfectly. But the problem that if I repeat this action he will add the consultations. On the first click it sends an ajax request, if I click again it sends two ajax requests, if I click again it sends 3, and so on... what would be causing this? inserir a descrição da imagem aqui He made 6 appointments at once. As it was working I only noticed it after a while.

If it’s some silly mistake I beg your pardon, I’m still a beginner.

  • First, your code becomes easier to read. Second, the function searchTradeItems you only call once? Third, when you say "On the first click...", what event does it trigger? Or rather, what button is it clicking on?

1 answer

3


Declare this function outside the function searchTradeItems().

$("#alert-ok").click(function() {
    var iquality = $("#alert-value").val();
    $("#alert-box").fadeOut(200);
    $.ajax({
        url: "systems/action-trade.php",
        type: "POST",
        data: {
            uid: uid,
            iusername: iusername,
            itemType: itemType,
            valueType: valueType,
            userid: userid,
            iname: iname,
            iquality: iquality
        },
        beforeSend: function() {},
        success: function(result) {
            $("#user-painel-2").html(result);
            $("#alert-value").val("");
        }
    });
}); 

function searchTradeItems() {
    var uid = $("#useruniqid").val();
    var iusername = $("#username").html();
    var userid = $("#balmung-id").val();
    var itemType = $("#trade-type option:selected").val();
    var valueType = $("#trade-gold-type option:selected").val();
    if (valueType == 5) {
        $("#alert-box").show();
        $("#mensagem-alert").html("Insira o valor da qualidade entre 0 e 100");
        $("#alert-cancel").click(function() {
            $("#alert-box").fadeOut(200);
        });
    };
}
  • I updated my POST

  • 1

    Good Gumball, now it was... So it was because I declared inside the searchTradeItems? Anyway it was worth the help!

  • 2

    @Jacksonantunes No! It’s because every click you gave him saved the event and replicated it at the next click. Because every time you opened the modal you declared the function. So it was as if you had several functions of the same. If solved, mark as solved, please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.