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

219
Vistas
ajax call still posts to the data after clicking Cancel on alert box. Why?

I have several ajax calls that posts data to the database through webmethods when the submit button is clicked.

When a user clicks the submit button, an alert box pops up asking the user to verify his/her order before submitting to the database.

When everything looks good and the user clicks the submit button, data is successfully posted to the database.

This part is working fine.

The issue is when the user clicks Cancel, data still posts to the database although this time, blank records get posted to the database.

My question is why is data still getting posted when the user clicks cancel?

Clicking the Cancel option should not trigger ajax call to fire.

What am I missing?

Here is relevant code. More code will be provided if requested.

function getAllCreditorData() {
        var data = [];
        $('tr.data-contact-person8').each(function () {
                var creditname = $(this).find('.creditorname01').val();
                var creditaddress = $(this).find('.creditoraddress01').val();
                var creditincome = $(this).find('.creditorincome01').val();
                var alldata = {
                        'myCreditorname': creditname,
                        'myCreditoraddress': creditaddress,
                        'myCreditorincome': creditincome
                }
                data.push(alldata);
        });
        console.log(data);
        return data;
}

$("#btnSubmit").click(function (event) {
        event.preventDefault();
        if (confirm('Please review your order before submitting. Click Ok to submit; Cancel to make additional changes')) {
                var empComplete = false, sourceComplete = false, spouseComplete = false, dividentComplete = false, reimbursedComplete = false, honorariaComplete = false, giftComplete = false, orgComplete = false, creditorComplete = false;
                function checkComplete() {
                        if (empComplete && sourceComplete && spouseComplete && dividentComplete && reimbursedComplete && honorariaComplete && giftComplete && orgComplete && creditorComplete) {
                                $("#result").text("Thank you! You have successfully completed this form");
                        }
                }
        }
        $("#result").text("");
        var data = JSON.stringify(getAllEmpData());
        console.log(data);
        $.ajax({
                url: 'disclosures.aspx/SaveEmpData',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ 'empdata': data }),
                async: false,
                success: function () {
                        empComplete = true;
                        checkComplete();
                },
                error: function () {
                        alert("Error while inserting data");
                }
        });

Thanks for your help

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Your ajax call is outside of your if condition.

$("#btnSubmit").click(function (event) {
    event.preventDefault();
    if (confirm('Please review your order before submitting. Click Ok to submit; Cancel to make additional changes')) {
        $.ajax({
            url: 'disclosures.aspx/SaveEmpData',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ 'empdata': data }),
            async: false,
            success: function () {
                    empComplete = true;
                    checkComplete();
            },
            error: function () {
                    alert("Error while inserting data");
            }
    });

            var empComplete = false, sourceComplete = false, spouseComplete = false, dividentComplete = false, reimbursedComplete = false, honorariaComplete = false, giftComplete = false, orgComplete = false, creditorComplete = false;
            function checkComplete() {
                    if (empComplete && sourceComplete && spouseComplete && dividentComplete && reimbursedComplete && honorariaComplete && giftComplete && orgComplete && creditorComplete) {
                            $("#result").text("Thank you! You have successfully completed this form");
                    }
            }
    }
    $("#result").text("");
    var data = JSON.stringify(getAllEmpData());
    console.log(data);
over 4 years ago · Santiago Trujillo Denunciar

0

You should enclose the $ajax code inside the if statement that asks the user to confirm() whether they want to submit or not.

Your if statement checks for user confirmation. But the code of the ajax call us not inside the if statement parenthesis. It's outside them. So it will run whether the statement is found true or not.

-RH

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