0

I'm looking for a script to prevent the user from hitting the submit button twice specially for long time background processing forms. I'd like to implement something not just functional but visually appealing too. Can anybody share some tips of how to accomplish this using, for instance, scriptaculous ?

Thanks

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
xain
  • 13,159
  • 17
  • 75
  • 119

2 Answers2

1

Using prototype you can try something like this:

var DoubleSubmit = {
prevent: function(){
    var forms = document.body.select('form');
    forms.invoke('observe', 'submit', DoubleSubmit.observeSubmit);
},
observeSubmit: function(event){
    var submitButtton = event.element().select('input[type="submit"]')[0];
    submitButtton.disable();
    return true;
}};document.observe('dom:loaded',DoubleSubmit.prevent);
voytee
  • 11
  • 2
0

It's not scriptaculous, but I always did it like this:

HTML:

<form onsubmit="return checkDoubleSubmit(this) && yourSubmitroutine();">
  <input ...>
   ...
  <input type="hidden" value="0" id="dscheck">
</form

Javascript:

function checkDoubleSubmit(form) {
    var v = 0;
    var e = form.getElementById("dscheck");
    if (e != null) {
        v = e.value;
        v = v * 1;  // Coerce to number
        e.value = "1";
    }
    return v == 0;
}
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71