Search code examples
javascripthtmlsweetalert2

loading progress form submit using sweetalert


I would like to render a waiting time animation when submitting a form, but I prefer to use SweetAlert rather than a standard loading image.

This the basic code:

$("form").submit(function (e) {
            e.preventDefault(); // stops the default action
            $("#loader").show(); // shows the loading screen
            $.ajax({
                url: test.php,
                type: "POST"
                success: function (returnhtml) {
                    $("#loader").hide(); // hides loading sccreen in success call back
                }
            });
        });

This is the code SweetAlert wants to implement:

window.swal({
  title: "Checking...",
  text: "Please wait",
  imageUrl: "images/ajaxloader.gif",
  showConfirmButton: false,
  allowOutsideClick: false
});

//using setTimeout to simulate ajax request
setTimeout(() => {
  window.swal({
    title: "Finished!",
    showConfirmButton: false,
    timer: 1000
  });
}, 2000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" />

plz someone make the tips for doing this


Solution

  • You were almost there! Just replace the #loader code with the sweet alert code.

    $("form").submit(function (e) {
                e.preventDefault(); // stops the default action
                //$("#loader").show(); // shows the loading screen
                window.swal({
                  title: "Checking...",
                  text: "Please wait",
                  imageUrl: "images/ajaxloader.gif",
                  showConfirmButton: false,
                  allowOutsideClick: false
                });
                $.ajax({
                    url: test.php,
                    type: "POST"
                    success: function (returnhtml) {
                        //$("#loader").hide(); // hides loading sccreen in success call back
                        window.swal({
                          title: "Finished!",
                          showConfirmButton: false,
                          timer: 1000
                        });
                    }
                });
            });