简体   繁体   中英

SweetAlert with Java Servlet

I wanted to pause form's onsubmit, so I could ask user to confirm action before proceed. So here's my form:

            <form action="<%= request.getContextPath()%>/Controller" 
                  method="POST" id="remove_book" 
                  onsubmit="alertBookInfo('return_book')">
            </form>

Then, I created the JavaScript function, using SweetAlert:

function alertInfo(action) {
document.querySelector(action).addEventListener('submit', function (e) {
    var form = this;
    e.preventDefault();
    if (action === "remove_book") {
        swal({
            title: "Remove book?",
            text: "Watch out",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes.",
            cancelButtonText: "No.",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                swal({
                    title: "Deleted.",
                    text: "Done.",
                    type: "success"
                }, function () {
                            form.submit();
                });
            } else {
                swal("Cancelled", "Not done.", "error");
            }
        });
    }
});
}

But for some reason, I am unable to prevent page reload on form submit. Am I doing someting wrong?

PS: I already tried with return alertInfo() in form and returning boolean value from JS function with no success.

If you don't want the page reloads you could use an AJAX call. When you use the submit() you will always reload the page because is how submit works.

$.ajax({
        method: "POST",
        url: YOUR_ACTION_URL,
        data: $('form').serialize(),
        success: function(data){
            //here you could add swal to give feedback to the user
        }
    });

In your controller you have to define the method as produces JSON, if you are using Spring, the annotation is @ResponseBody

Why not call the function from the form like this, the the alert will be prompt before submitting the form:

HTML

<form action="<%= request.getContextPath()%>/Controller" method="GET" id="remove_book" onclick="myAlertFunction(event)">
  <input type="submit">
</form>

JavaScript

function myAlertFunction(event) {
  event.preventDefault()
  swal({
      title: "Remove book?",
      text: "Watch out",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes.",
      cancelButtonText: "No.",
      closeOnConfirm: false,
      closeOnCancel: false
    },
    function(isConfirm) {
      if (isConfirm) {
        swal({
          title: "Deleted.",
          text: "Done.",
          type: "success"
        }, function() {
          $("#remove_book").submit();
        });
      } else {
        swal("Cancelled", "Not done.", "error");
      }
    });
}

And if you want to prevent the reload, you can always use event.preventDefault()

Here is an example: JSFiddel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM