Cookie to remember drop down box

August 18, 2009 at 13:16:00
Specs: 2000 pro, -
Hello,

I am look for a cookie script (php or JavaScript) that will create a cookie of the users choice in a HTML drop down box and then when the user revisits the page the cookie will select that option at the start.

Thanks.


See More: Cookie to remember drop down box

Report •


#1
August 19, 2009 at 12:16:04
Something like below *may* work for javascript. php would be similar, but you'd need to get the proper index first, and then write the page on the fly.

<html><head><script type="text/javascript"><!--

function chkCookie() {

   var d = document.getElementById('sel');
   var c = document.cookie;

   if(c.length > 0) {
     if(c.indexOf("my_cookie_name=") != -1) {
        var idx = parseInt(c.substr(c.indexOf("=") + 1) - 1);
        d.selectedIndex = idx;

        delete idx;
     }      
   }

   delete d;
   delete c;
}

function setCookie(val) {

   var d = new Date();
   d.setDate(d.getDate() + 30); // 30 day cookie
   document.cookie = "my_cookie_name" +
                     "=" +
                     escape(val) +
                     "; expires=" + d.toGMTString();

   delete d;
}

//--></script></head>

<body onload="chkCookie()">

<select id='sel' onchange='setCookie(this.value);'>
<option value='1'>option 1</option>
<option value='2'>option 2</option>
<option value='3'>option 3</option>
</select>

</body>
</html>

HTH


Report •

#2
August 20, 2009 at 11:00:00
I have tried this, and it dosen't work.
Any more help,
Thanks

Report •

#3
August 20, 2009 at 14:10:19
What code did you use and on what browser?

Here's a php version you can try... hopefully it will work. :)

<?php

   $idx = null;

   if(isset($_POST["setc"])) {
      setcookie("test_cookie", $_POST["sel"], time() + 3600 * 24 * 30);
      header("location: " . $_SERVER["PHP_SELF"]); 
   } else if(isset($_COOKIE["test_cookie"])) {
      $idx = $_COOKIE["test_cookie"];
   } 
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<select name="sel" size="1">
<option value="1"<?php echo (!is_null($idx) && $idx === "1" ? " selected" : "");?>>Option 1</option>
<option value="2"<?php echo (!is_null($idx) && $idx === "2" ? " selected" : "");?>>Option 2</option>
<option value="3"<?php echo (!is_null($idx) && $idx === "3" ? " selected" : "");?>>Option 3</option>
</select>
<input name="setc" value="save setting" type="submit">
</form>

HTH


Report •

Related Solutions

#4
August 23, 2009 at 08:38:14
Thanks for your help.
The PHP one works now.

Report •

#5
September 20, 2009 at 02:48:43
I want to add a "REMEMBER ME" check box with this Dropdown list, so that the site remembers the option choosen earlier.

I wish to change the Go Button with a image Button as well.

Please Help me

I've the script here :

<select name="menu">
<option value="http://www.yahoo.com">Text that will appear in the choices</option>
<option value="URL">Text that will appear in the choices</option>
<option value="URL">Text that will appear in the choices</option>
<option value="URL">Text that will appear in the choices</option>
<option value="URL">Text that will appear in the choices</option>
</select>
<input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="GO">
</p>
</form>



Report •

#6
September 21, 2009 at 13:23:20
For the checkbox, you can try something like

Remember Me <input name="remember" type="checkbox" checked>

I'm not much into web design, but IIRC, the syntax for the image is something like

<input type="image" src="img_source/image.gif" border="0" />

Good luck.


Report •


Ask Question