Avoid submitting multiple forms in html

I ran into some kind of fictitious problem.

My site has an order form (simple html form), and I noticed that from time to time I get double commands.

I realized that if I pressed the submit button several times (before loading the action page), I had as many commands as I pressed.

So I wonder if there is a simple solution to make the form submission asynchronous?

thank

PS I added a jQuery UI dialog box to send "wait please ...", but I get two more commands.

UPDATE

As GeoffAtkins suggested:

  • disable sending after displaying a dialog box
  • use a unique form token (since it has already been added by Symfony) Do not use a Symfony token as a unique form token, since it always matches the current session. Use just random or something like that.
+4
source share
4 answers

I would think about it (jQuery since you said you used it)

$(function() {
  $("#formId").on("submit",function() {
    $("#submitBut").hide();
    $("#pleaseWait").show();
  });
});

if you submit the form and reload the page.

If you are Ajax, then do

$(function() {
  $("#formId").on("submit",function(e) {
    e.preventDefault();
    var $theForm = $(this);
    $("#submitBut").hide();
    $("#pleaseWait").show();
    $.post($(this).attr("action"),$(this).serialize(),function() {
      $theForm.reset();
      $("#submitBut").show(); // assuming you want the user to order more stuff
      $("#pleaseWait").hide();
    });
  });
});

NOTE that disabling the submit button when you click the submit button can completely stop sending (at least in Chrome): https://jsfiddle.net/mplungjan/xc6uc46m/

+2
source

Just turn off the button when pressed, something like:

$("#my-button-id").on("click", function() {
   $(this).attr("disabled", "disabled");
});
0
source
var bool = true;
function onclick()
{
    if(bool)
    {
    //do stuff
    bool = false;
    }
    else
    {
    //ignore
    }
}
0

, . , , "..." .

You can also re-enable the button on error or execute an ajax request.

I have done this many times: fooobar.com/questions/1172309 / ...

0
source

All Articles