Click to See Complete Forum and Search --> : cookies with dropdown list
Josh IC
01-21-2003, 08:10 AM
Hi. I'm new with javascripts and cookies, and have a question. I am trying to set a cookie by selecting a drop-down list. The trick is that I don't want to set it when the user hits the submit button of the form, but I want it to set when the option is selected. I did this for text fields by setting the onChange value to "SetCookie('name','form.name.value')", but that doesn't seem to work with drop downs. Can someone please let me know how to do this or point me in the direction of a good resource?
Thanks,
Josh
I think this will work for you..
Add your <option> tag like this. Note: change setcookie to the name of your cookie function...<option name=menu size=1 onChange="setcookie()">Then to get the value of the dropdown list use this.var formvalue = document.form.menu.options.value;and then write your cookie like this....document.cookie = "name="+formvalue+";expires...Hope that all makes sence, if not...let me know.
Josh IC
01-21-2003, 09:16 AM
...sorry about the double post ...
Josh IC
01-21-2003, 09:33 AM
Thanks for the help, but it didn't quite work ...
I'm gonna post some code here, maybe you can show me what I'm doing wrong. Also, it's a little more complicated than I originally said, cause I'm also using php, and actually filing in the drop down using php and mysql. Anyway, here's the declaration of the drop down list :
<select name='class' id='class'>
<option value='default' SELECTED>Select a Class</option>
<?php
$sql = "SELECT name FROM class ORDER BY name";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result)) {
$name = $myrow["name"];
?>
<option size=1 value="<?php echo "$name"; ?>" onChange="SetCookie('class', document.form.class.options.value);"><?php echo "$name"; ?></option>
<?php
}
?>
</select>
I know that my setCookie and GetCookie functions work, cause I've tested them with regular textfields, and haven't had any problems.
Can someone please help me out ..
Thanks,
Josh
I think that your problem is becaues you are using PHP to name your option field.
document.form.menu.options.value needs to be document.nameofform.nameofoption.options.value. You could try this....
document.nameofform.<?php echo "$name"; ?>.options.value and see if that will work...
Josh IC
01-21-2003, 10:08 AM
Hi again.
Your code still didn't work ... it didn't even work when I tried to just put 'default' as a value to set for the cookie, as seen here :
<option size=1 onChange="JavaScript:SetCookie('class', 'default' );" value="<?php echo "$name"; ?>"><?php echo "$name"; ?></option>
But i did figure something out. When I put that onChange="....." in the <select> tag, it does set a cookie. Here's some of that code :
<select name='class' id='class' OnChange="JavaScript:SetCookie('class', 'default' );">
That would be good enough, but I don't want to cookie to be set to 'default', I want it to have the selected value. Maybe you know a way to work with this?
Thanks so much for all of your help, I really appreciate it.
Josh
Does it work to change 'default' to '<?PHP echo "$name"; ?>'?
Josh IC
01-21-2003, 12:06 PM
Hi, thanks for all of your help...but i figured it out myself eventually.
here's what i did, in case your curious.
<select name='class' id='class' OnChange="JavaScript:SetCookie('class', this.value );">
<option value='default' SELECTED>Select a Class</option>
<?php
$sql = "SELECT name FROM class ORDER BY name";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result)) {
$name = $myrow["name"];
?>
<option size=1 value="<?php echo "$name"; ?>"><?php echo "$name"; ?></option>
<?php
}
?>
</select>
if you look, all i had to do was SetCookie('class', this.value ), and that get's the currently selected value out of the drop down and places it into a cookie called class.
-Josh
Sorry I couldn't be more help...I was just going off of the top of my head. Didn't have time to check any of it. Anyway, glad you finally got it.