/    Sign up×
Community /Pin to ProfileBookmark

Using cookies to save a drop down list value?

Hi folks,
So I’m a Rails / XHTML guy who writes no Javascript. However, I recently inherited a client with a PHP-based website who needs what should be a simple Javascript addition.

There’s a drop-down list that visitors submit when they show up on the site. I need to write some Javascript to produce a cookie that saves the drop-down selection. The idea is that next time the user visits the website, the drop down is selected. This should be really easy but again, I’m not a Javascript guy.

Any help would be greatly appreciated! Thanks!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meJul 24.2009 — i just answered a related question on another forum.

while this solution might be overkill for you, it would work as expected.

here is my other post:

you can store it in a javascript-accessible client-side storage interface like localSettings, globalSettings, or as a last resort, cookies.

my mini library (http://danml.com/pub/lib/mini5.js) includes a function called store().

you can use it to save and load data between pages visits.

in IE6 you get 20kb, which should be enough to save a form or two.

in ie7 you get 100kb.

in ie8, and safari4, and FF2+ you get 5mb of space!

here is an example of how to use this function:

[CODE]<BODY onload="restoreForm()">

<form name="myform">
Input1:<input type="text" name="box1" onchange="store(this.name, this.value);"><br>
<input type="button" name="add1" value="Add1" >
</form>

<script type='text/javascript' src='http://danml.com/pub/lib/mini5.js'></script>
<script type='text/javascript'>
function restoreForm(){
var allFields=tags("input").concat(tags("select"));
allFields.map(function(a) {
if (a.name && {select: 1, text: 1}[a.type.toLowerCase()] ) {
var tmp = store(a.name) || "";
if (tmp) {
if (a.options) {
a.selectedIndex=Number(tmp)||0;
} else {
a.value = tmp;
}
}
}
});
}//end restoreForm()

</script>
</BODY>
[/CODE]

(tested FF3.5)

if it's too much, you can change store() to getCookie()/setCookie(), and keep the rest of code shown.

i have get/set cookie in the same lib as linked, and in my snippets like as well.

that way, it would use real cookies, and you wouldn't need the whole mini5 lib...

although then you would need to replace tags() and ensure that Array.map() is available...
×

Success!

Help @greenguy109 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...