Problem showing popup when sending forms

Asked

Viewed 44 times

0

I developed a form, when the user clicks on the send button a GIF appears and then a modal has to appear saying that the information has been sent so far all right the GIF works normal but when the form is submitted the GIF goes away and the Popupdoes not appear like to know if I am doing something wrong follows my Javascript code:

 $("#submit-btn").click(function (e) {
    e.preventDefault();

    var data = $('form').serializeArray();

    if ($(this).hasClass('btn-warranty')) {
        var inputRangeValue = $('.final-value').first().text();
        var specialty = {name: 'specialty', value: $('#specialty').val()};
        var value = {name: 'value', value: inputRangeValue + '000'};
        data.push(specialty);
        data.push(value);
    }

    if ($(this).hasClass('btn-indenizometro')) {
        var question_1 = $('input[name=question_1]:checked').val();
        var question_2 = $('input[name=question_2]:checked').val();
        var question_3 = $('input[name=question_3]:checked').val();
        //var reason =  {name: 'reason', value: sessionStorage.tipo };
        var monthly = {name: 'monthly', value: $('#monthly-value').text()};

        data.push(question_1);
        data.push(question_2);
        data.push(question_3);
        data.push(monthly);
        //data.push(reason);
    }

    var product = {name: 'product', value: window.location.pathname};
    data.push(product);
    $('.loaderImage').show();
    //Ajax post data to server
    $.ajax({
        type: 'POST',
        url: '/request',
        data: data,
        dataType: 'json',
        success: function (data) {
            $('.loaderImage').hide();
            $('#modal-success').modal('show');
            $('form')[0].reset();
        },
        error: function (data) {
            //console.log(data);
        }
    });
});

NOTE: just remembering that the modal I’m using is from boostrap

  • What you want to do with a modal within a callback jquery?

  • 1

    type when I click to send appears a gif on the button because it takes too long to send the form I implemented this gif more ai when the form and sent it adds up then the popup would have to appear more it does not appear

  • Open the Chrome console and see if there is any error. See this simulation that I did. Apparently your code has nothing wrong. Exchange .click() for .on( "click", function () { ... });

  • I will add the following: using modals is a bad idea for notification. They are intrusive and get in the way, and it’s an extra click for me, the user. Use something like Notifyjs, much more elegant and it’s just a notification.

1 answer

1

Your logic is not wrong, but I believe this is a loading problem where the id $('#modal-success') was not recognized.

$(document).ready(function(){
        $(#botao).click( function(){
            $('.loaderImage').show();
            //Ajax post data to server
            $.ajax({
            type: 'POST',
            url: '/request',
            data: data,
            dataType: 'json',
            success: function (data) {
                $('.loaderImage').hide();
                $('#modal-success').modal('show');
                $('form')[0].reset();
            },
            error: function (data) {
                //console.log(data);
            }
        });
    });
});

The event ready triggers the loading of Javascript code after the entire HTML document is loaded, thus preventing any element from being recognized. I did some home testing based on your example and it worked

  • friend I will upgrade my Code to see be all key as it is

  • ta la atualizei o js

  • This code is in right separate file?

  • that’s even in a file called app.js

  • So now take all the code from this file and put it into $(Document). ready( Function(){ ...all its code });

  • If he put app.js at the end of the document, before closing body (</body>), that’s not necessary.

Show 1 more comment

Browser other questions tagged

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