View Full Version : Settin cookie from list box ?
Nomadicus
12-01-2002, 07:11 PM
I have a list box on a form that allows the user to choose from one of three languages : English, French or German :
<SELECT Name = "Language">
<OPTION>Deutsch
<OPTION>English
<OPTION>francais
</SELECT>
After they make their selection they can also click on a button that will send them a cookie (which will be used for what default language they want) as thus :
<INPUT TYPE="button" NAME="theLanguage" VALUE = "Set" onclick = "set_cookie('LanguageChoice', this.form.Language, '1000')">
But the cookie only sees an object here for a value, not the actual choice from the list box. How can I set the cookie with the proper list box selection ?
Thanks in advance.
ConfusedOfLife
12-01-2002, 07:31 PM
Try this :
<form>
<select name="language">
<option value="Deutch"> Deutch
<option value="English"> English
<option value="Francais"> Francais
</select>
<input type="Button" value="Set" onclick="alert(this.form.language.value)">
</form>
Don't forget to change that alert!
Note : Even though you're not using escaped characters in your values, but its always a good idea to escape your names and values for your cookie file! Don't ask why!!
Nomadicus
12-01-2002, 07:55 PM
Thanks, I see I was wrong on two counts there. Kinda hard to pass on values when you forget to assign any. :)
BTW, what did you mean by escaping the characters in the values? Do you mean as :
<OPTION value=\"Deutsch\">Deutsch
Nomadicus
12-01-2002, 08:07 PM
Ok, we have the list box as thus :
<form>
<select name="Language">
<option value="Deutch"> Deutch
<option value="English"> English
<option value="Francais"> Francais
</select>
<input type="Button" value="Set" onclick="set_cookie('LanguageChoice', this.form.Language.value, '100000')">
</form>
Now that they have set the cookie, once I retrieve it, I want to make the list box show their default selection, e.g.
<option selected value="English"> English
But how can I work the value from the cookie into the list box code? I think I could do this in PHP, but I'd like to stick to JS here for continuity sake.
I tried enclosing the entire list box between the <script> tags but it blew up on me.
Thanks.
ConfusedOfLife
12-01-2002, 08:19 PM
Hi, no! I didn't say that you do anything for your values in the <option> tag, but when you write into the cookie, you have a name ( the left side of your expression or what you assign to) and a value ( the opposite of the prev. parenthesis!). Also you should know that you're not allowed to enter any spaces in the cookie file, therefore writing something like the following is wrong:
setCookie("name", "my name", 1000) ;
So, what can you do to have your spaces written? One way is to encode them by your own choice ( like puttin &mickmick!) instead of each space and of course when you get the file you have to decode it by your own decoding function. But it's no need to go for all this, as JS has escape and unescape predefined functions, it's like urlencode in PHP, and for example it changes a space to %20 as you can notice in the url path of many sites ( you can see this even in Windows when you right click on the name of a file that contains spaces in its name, and then you choose propery, for example "my image.gif" looks like "my%20image.gif").
Also do not use that 1000 for the expire date, you should enter your date in GMT, what I do for keeping my scripts alive for a long time is to get the current month, add 120 to it ( 2 years ), convert the date to GMT and then put it as my expire date, you can use this function:
function DesiredDate(when)
{
expireDate = new Date();
if ( when == "Future" )
expireDate.setMonth( expireDate.getMonth() + 120 );
else
expireDate.setMonth( expireDate.getMonth() - 1 );
return expireDate;
}
Also you can use this function for setting your cookie:
function setCookie(Name, Value, Expires)
{
document.cookie = escape(Name) + "=" + Value + "; " +
( (Expires) ? ("expires=" + Expires.toGMTString() ) : "");
}
So, for setting a simple cookie to live for 2 years, we write this:
setCookie("myName", "Hello It's nothing wrong with entering spaces!", DesiredDate("Future"));
kwhubby
12-01-2002, 08:32 PM
you can also put non break spaces instead of normal spaces such as this one in the quotes (just copy and past) '_' (alt + 0160) it lookes like the normal space, but its nonbreaking so it will not be converted to %20, and it acts as if it were a charater, and not a space.
edit!!!!: ahhh, this forum converted it into a underscore !!. so dont use it, just make one by pressing alt + 0 1 6 0 (on the num pad)
ConfusedOfLife
12-01-2002, 08:33 PM
Set qValue to your cookie value and use this :
qValue = "Francais"; // Your cookie value, I put this for testing only!
for (i=0; i < document.forms[0].language.options.length; i++)
if ( document.forms[0].language.options[i].value == qValue)
document.forms[0].language.options[i].selected = true;
I would write the index of the chosen option to the cookie personally, assuming the chosen option was 2 ( "Francais"), you can select it again like :
document.forms[0].language.options[qValue].selected = true;
glenngv
12-02-2002, 02:42 AM
Originally posted by ConfusedOfLife
Try this :
<form>
<select name="language">
<option value="Deutch"> Deutch
<option value="English"> English
<option value="Francais"> Francais
</select>
<input type="Button" value="Set" onclick="alert(this.form.language.value)">
</form>
Don't forget to change that alert!
Note : Even though you're not using escaped characters in your values, but its always a good idea to escape your names and values for your cookie file! Don't ask why!!
the cross-browser way to get the select's value is:
document.formname.selectname.options[document.formname.selectname.selectedIndex].value
so the code should be:
<input type="Button" value="Set" onclick="alert(this.form.language.options[this.form.language.selectedIndex].value)">
Nomadicus
12-05-2002, 05:47 PM
I have tried using both :
<input type="Button" value="Set" onclick="alert(this.form.language.value)">
and . . .
<input type="Button" value="Set" onclick="alert(this.form.language.options[this.form.language.selectedIndex].value)">
but in either case the alert dialog displays nothing.
I've carefully checked my code for correct syntax. The list box is called "language".
Any suggestions?
wyattwebb
12-05-2002, 07:45 PM
So this is what I've tried and it seems to work...
Try the things in bold
<form>
<select name="language" id="language">
<option value="Deutch" selected> Deutch
<option value="English"> English
<option value="Francais"> Francais
</select>
<input type="Button" value="Set" onclick="alert(this.form.language.options[this.form.language.selectedIndex].value)">
</form>
What Browser are you using???
ConfusedOfLife
12-06-2002, 09:10 AM
<form>
<select name="language" id="language">
<option value="Deutch" selected> Deutch
<option value="English"> English
<option value="Francais"> Francais
</select>
<input type="Button" value="Set" onclick="alert(this.form.language.options[this.form.language.selectedIndex].value)">
</form>
Ths Id shouldn't make any difference unless you're using it somewhere else.
Nomadicus, when I wrote it it's working! ( I always check and then post) and since it's a long time for a reply, it's better that you send the whole code that we see what you're doing.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.