prevent multiple form submission on keyup ajax post -


i working on form retail outlet in need submit form on enter keyup , redirect form same page. on enter keyup ajax post being submitted php function , on successful data insert doing page redirect. able currently, in few instances users hitting enter key multiple times (may 3 4 times in sec) because of being submitted multiple times. how prevent multiple submission. have tried generate random number , pass session token but, each submission generating new random number. here code

    $(document).keyup(function(e){     if (e.keycode == 13) {          var postdata = $("#invoiceform").serialize();             $.ajax({             url: "addsale()",             type: "post",             data: postdata         });         e.preventdefault();         return false;     } }); 

use flag variable ignore new request if not completed previous , work lock.

var isonway=false; $(document).keyup(function(e){ if (e.keycode == 13) {      var postdata = $("#invoiceform").serialize();         $.ajax({         url: "addsale()",         type: "post",         before:function(){                if(isonway)                   return false; // ignore request                isonway=true;// start lock flag                 },         complete:function(){                  isonway=false; //free                 },         data: postdata     });     e.preventdefault();     return false; }  }); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -