views:

353

answers:

3

In my form i need to select an item (which is a div with text or an image) and i do set the id of a hidden input via jquery live click event.

In the case the user has logged out or session has expired he must go back and refresh the page (after logging in). After a refresh firefox keeps all input data (textarea, text, select, etc). BUT the value i set in the hidden input is reset back to 0 and the user must click the item again.

How can i make javascript remember after a page refresh? Everything else is remembered after a refresh, its natural for the user to expect this is too instead of being annoyed every time some error occurs with his entry.

A: 

Sounds to me you can use cookies to store the selected value. Don't forget to clear once the form has been processed.

EDIT 1

If you have a situation when the user can submit it more time, you must prefix each cookie with an ID to denote the current form.
When the form is presented to the user first time, make a hidden field having the timestamp stored in it (This can be done on the server side). Then you can use this timestamp for the cookie prefix, so you will have cookies for the form that has been generated, and another timestamp for the 2nd form the user works with. You will have independent cookies for each form you open in your browser.
When you submit the page, the timestamp is sent to the server and you can setup cookies. If the user hits the browsers back button, it will go back to a form, where you already have the timestamp in your form's html source, so it will load the cookies for that form.

Pentium10
answer has already but accepted but what if the user submits 2 pages? only the most recent will have the correct info.
acidzombie24
See my edit to the question of using timestamp or microtime to store cookies against the current form.
Pentium10
i like it. But i see ONE last problem. If i hit refresh wouldnt i lose the timestamp? Theres no way for the server to know which page/form i refresh and would replace the timestamp. If its possible to hack the refresh button then i could add the timestamp to the url (or use the '#hash-data'). But hacking the refresh button feels like a hack and is probably browser dependent?
acidzombie24
If user hits refresh, he wants to get refresh. I think it's too much you want. You simply can't overwrite how a browser works.
Pentium10
+1  A: 

You can store some information in a cookie, or in the window.location.hash key or in the window.name

All these values will be kept when you refresh.

Mic
interesting. Would it have a weird side effect when the user clicks something? (when i set the hash?). I may do this.
acidzombie24
confirmed. when doing this in firefox and chrome it jumps when items are clicked.
acidzombie24
If you use already the hash for something else, it's better to use the other options.
Mic
hmm i think i understand what your saying. if i am using the hash? you mean if i using the val as an id? i am storing ids in there which now makes sense that its jumping to it. i forgot, +1
acidzombie24
+1  A: 

Try to use type="text" and style="display:none" instead of type="hidden"

Artic
genius. You beat out all the other answers hands down.
acidzombie24
But this will work only in Firefox isn't it?
Mic
@Daniel Vassallo: even if its browser-dependent it will be consistent since all or no data will be available. (unless some browsers purposely ignore text fields set by javascript which i dont know why they would)
acidzombie24