3

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.

pietrochico
  • 33
  • 1
  • 6

2 Answers2

0

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

cralfaro
  • 5,822
  • 3
  • 20
  • 30
  • Thanks. I know I can use AJAX but I don't want to: changing from an AJAX request from HTTPRequest would force me to change too many things on my project, so I hoped I could not use it. – pietrochico Jan 25 '17 at 09:55
  • I understand what you need, but I am afraid is not possible using a form, have a look here also to this post http://stackoverflow.com/questions/18169933/submit-form-without-reloading-page – cralfaro Jan 25 '17 at 10:01
  • So I could only show an alerts that informs user of page changes, so:
    and, the function would be the function you showed me, right?
    – pietrochico Jan 25 '17 at 10:05
  • Yes you can add this function, then you will have the total control of the form, so no more reload, then you can send all form data to the controller and by JS process the response and show the message you ned to the user. Also remember to change the button type from submit to button – cralfaro Jan 25 '17 at 10:07
  • Thank you. so much. By the way, how can standard alerts block page reload? – pietrochico Jan 25 '17 at 10:10
  • Is not because is standard alert, the one who reload the page is the submit action because with an action you always have to redirect to some page, is how to submit works, in the moment you use ajax, you use a different way, so you can decide how the page will act. If you think the response if clear enough could you validate it? so is usefull for some other people :) Thanks – cralfaro Jan 25 '17 at 10:14
0

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

Zorken17
  • 1,896
  • 1
  • 10
  • 16