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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies, I want to store a Users State of a Dropdown

Status
Not open for further replies.

ShawnDT

Technical User
May 1, 2006
67
0
0
US
Good Morning Group,

I know this is going to sound easy but, I want to store a users search preference. In a Dropdown. We have search fields, Plan Number, Plan Name, Plan type. You can search on any one of those. Most users like to use one or the other. What I want to do is store their like. So when they return to the page it automatically defaults to their preference. This has got to be an ez one but I cannot figure it out. I am not sure what to use to accomplish this. I have looked at cookies not quite sure cookies would work. Would I need to do this in a session variable. Code below.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<label>
<select name="mnuSearchField" id="mnuSearchField">
<option SELECTED>Plan #</option>
<option>Company Name</option>
<option>Plan Type</option>
<option>Plan Consultant</option>
</select>
</label>
</form>
</body>
</html>
 
I'm guessing by the first line of your code that you use ASP. If this is the case, you'd be much better off doing this server-side, and picking up on the cookie server-side to output the right option selected when the page is delivered. So, I'd ask in the ASP forum about this.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Easiest way I can think of doing this:

When they submit the form, get the selected index of the drop down and place it in a cookie

Code:
intVal= document.getElementById(mnuSearchField").selectedIndex;

When they load the page in your body's onLoad event, check to see if a cookie exists, if it does then fire this code:

Code:
document.getElementById("mnuSearchField").selectedIndex = <value of cookie>;

See here to learn how to write and read cookies, it's pretty straight forward:


Of course using this method will assume you don't change the order of the drop down items.

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Sorry - I shouldn't have said 'cookie' in my reply, as I don't want to assume that's the best way to do it in ASP (perhaps some sort of session storage might be better? *shrug*)



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top