views:

75

answers:

4

Well, everything's in the title but I'll explain a little more :-)

My rails app contain many forms (Ajaxified or not).

To prevent users to submit twice or more some forms, I use Javascript.

There's my scenario for a Ajaxified form :

  • the user submit the form (clic or enter)
  • the javascript disable the submit button
  • the rails controller do things (like a Soap request or an insert in a DB)
  • the rails controller update the page and enable the submit button if necessary (in case of errors)

Now I want to add server side code to keeps things really clean if the user bypass the javascript.

Any suggestions?

A: 

Not a real suggestion (I won't be surprised of downvotes), but will your site still be usable without JS? How about showing him appropriate message that for normal operation he needs to enable the JS, otherwise you won't show him lots and lots of forms on the page.

Eimantas
True and you made me edit my question :-) The user case I'm talking about is when the user bypass the javascript. Consider the javascript is always active. My question is mostly about the server-side part..
Fro_oo
A: 

1) It's nice to also show some moving indicator to let user know that something's going on, that their request is being processed. It should eliminate lots of double submits.

2) If user has disabled javascript, how're you gonna submit 'ajaxified' forms? If site becomes not functional without javascript, then it's probably best to just notify user (like Eimantas suggests).

edit One example of such indicator, just to be clear what I mean in 1.
http://www.netzgesta.de/busy/

Nikita Rybak
1) Sure, I'll implement one2) I've edited my question: the real problem is if the user bypass the javascript. (if the user disable the javascript, the form submitting is processed as usual.. there's no need to prevent double submit)
Fro_oo
A: 

Here's what I'd do:

  1. Add a "token" field to an object in db that's about to be changed.
  2. Put that token into a form that modifies said object.
  3. Right after modification save that object with NEW token.
  4. Use that token in other page views.

This will not prevent double submission, but at least will prevent the changes from second commit. I.e. when user submits the form second time the code will check the submitted token against the one in database and if they do not match - do not do an update to the object.

This also has a draw back for newly created objects (i.e. if user want's to create a comment or smth like that). But in this case you may want to check the creation time interval from same user and if it's less than, say, 5 seconds - you'd prevent the object from being "created".

Eimantas
Yep, that's also a good way to handle multiple entries.
Fro_oo
+1  A: 

Not sure if this is helpful:

ON SERVERSIDE:

  1. On fresh load of the form, set a session['variable']=false //meaning the form isn't submitted yet.

  2. On form submit, check:

    if session['variable'] == true
    {
       do nothing...
    }
    else
    {
       set session['variable'] = true;
      //do submit logic here
    }
    
jerjer