Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
After ajax submit, the message is repeated

When I run submit, an alert message occurs as many times as you send it before you reload.

For example.

first run submit occur success message

not refresh second run submit two repeat occurs success message

not refresh third run submit three repeat occurs success message

...

not refresh n run submit n repeat occurs success message

Why does it run like this? How can I solve this problem?

My code is as follows.

$(document).ready(function() {
            $('#my_modal').on('show.bs.modal', function(e) {
                var p_index = $(e.relatedTarget).data('p_index');
                $(e.currentTarget).find('input[name="p_index"]').val(p_index);

                $("button#submit").click(function() {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: "../receipt/send.php",
                        data: $('form.send_p_index').serialize(),
                        success: function(data) {
                            alert("success")
                            $("#send_p_index")[0].reset()
                            $("#my_modal").modal('hide');
                        },
                        error: function() {
                            alert("Error");
                        }
                    });
                });

            });
        }
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

WRONG

$(document).ready(function() {
            $('#my_modal').on('show.bs.modal', function(e) {
                var p_index = $(e.relatedTarget).data('p_index');
                $(e.currentTarget).find('input[name="p_index"]').val(p_index);

                $("button#submit").click(function() {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: "../receipt/send.php",
                        data: $('form.send_p_index').serialize(),
                        success: function(data) {
                            alert("success")
                            $("#send_p_index")[0].reset()
                            $("#my_modal").modal('hide');
                        },
                        error: function() {
                            alert("Error");
                        }
                    });
                });

            });
        }

CORRECT: Put this outside of modal.shown because every time the modal shown, it will write a submit function that repeats as many as it shown.

 $(document).on('click', 'button#submit', function() {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: "../receipt/send.php",
                        data: $('form.send_p_index').serialize(),
                        success: function(data) {
                            alert("success")
                            $("#send_p_index")[0].reset()
                            $("#my_modal").modal('hide');
                        },
                        error: function() {
                            alert("Error");
                        }
                    });
});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda