Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need explanation on form behavior re: state 1

Status
Not open for further replies.

gerikes

Programmer
Jun 5, 2007
5
US
Hi all.

I'm creating an xhtml form on a page for my company's intranet site. One of the entires of the form is a number that represents a key of a customer. Because there are many customers, I've created a popup window (using jquery & thickbox) with another form to allow users to search for the key by customer name. However, the user never sees the key, it's placed directly into the form. Basically, they search for the name, and see a bunch of results (names of companies). When they click a result, then the thickbox window disappears, the name of the company is shown on a label in the form (which isn't part of the form's data, that is to say, it doesn't have a "name" attribute), but the key is stored into a hidden input (which IS part of the form's data).

This works well when a user comes to the page the first time, uses the popup tool to find a customer and the key and name are both placed into their respective areas. However, because the customer name is stored on a label not part of the form, whereas the key is stored on the form, there are times when the user might come back to the page to have the form values still saved.

For example, a user first reaches the page, which means my values are as follows:

Hidden Key Value: 0
Customer Name: --NONE--

Now, when the user uses the popup tool to find a customer, they click one, and now the fields are set as...

Hidden Key Value: 6344
Customer Name: Joe Customer

They finish filling out the rest of the form, and hit submit. Now, for some reason, they decide to click back. Here is what the values are...

Hidden Key Value: 6344
Customer Name: --NONE--

Obviously, the key and customer name do not match up, which could cause incorrect entries.

My question, then, is what causes this? Is this a W3C standard behavior, or just something that a bunch of browsers do? And, more importantly, how can I disable it? I would like to have the fields reset to blanks and "nones".

Also, is there perhaps a better way of doing this? I just did it based on my own free will, not really researching if there was a better "best practice".

Thanks in advance!
 
It is standard behavior to keep values in inputs when you return to a page. It can get annoying when you have to go back, and everything is blank so you to have fill everything again. In lengthy forms this is even encouraged, by actually reloading the values into the form fields upon return to the page.

However you can have the form be blank using JS to reset all form fields to empty this includes hidden values.

Code:
<body onload="document.Formname.reset()">



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for the fast response and tip!

So, if standard behavior is to have the form be filled, then probably the best solution would be to go and fetch the name of the customer that relates to the key the form kept?
 
You could do that, or store the name in a session variable, and then just repopulate the label from the variable. That way you don't have to go through the process of querying, and retrieving for something that might not get used.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Since I found this thread high in a google search, I thought I'd explain what I did to solve my problem.

Basically, the problem I would encounter is that the browser would cache the page, but only the text parts, thus the label would not be saved. After searching around I thought to disable browser caches, use cookies, etc. Session variables wouldn't work because the page is loaded from cache, and is not the solution I was quite looking for.

I thought originally that this problem with the label not being saved was form-specific (i.e. the browsers would only save the parts of the page related to the form). Actually, it's a matter of inputs. It wouldn't save the label, not because it's not part of the form, but because it's just a label.

So, rather than use a label to display the name, I instead use a text field set to read-only. Since I never liked the idea of a text box that's read-only (since most users see a text box and see "I can change that") I use CSS to remove the border from the text box, which makes it look (as far as I can tell) exactly like a label. Because it's a text box,it will be stored in the cache alongside the key. I don't need to include it as part of the form so the form will still only submit using the hidden key input field.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top