Computing.Net > Forums > Web Development > Cookie to remember drop down box

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Cookie to remember drop down box

Reply to Message Icon

Name: Chris (by Chris Kirk)
Date: August 18, 2009 at 13:16:00 Pacific
OS: 2000 pro
CPU/Ram: -
Product: - / -
Subcategory: General
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: shutat
Date: August 19, 2009 at 12:16:04 Pacific
Reply:

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


0

Response Number 2
Name: Chris (by Chris Kirk)
Date: August 20, 2009 at 11:00:00 Pacific
Reply:

I have tried this, and it dosen't work.
Any more help,
Thanks


0

Response Number 3
Name: shutat
Date: August 20, 2009 at 14:10:19 Pacific
Reply:

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


1

Response Number 4
Name: Chris (by Chris Kirk)
Date: August 23, 2009 at 08:38:14 Pacific
Reply:

Thanks for your help.
The PHP one works now.


0

Response Number 5
Name: rogergrippo
Date: September 20, 2009 at 02:48:43 Pacific
Reply:

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>



0

Related Posts

See More



Response Number 6
Name: shutat
Date: September 21, 2009 at 13:23:20 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Cookie to remember drop down box

Drop Down Box www.computing.net/answers/webdevel/drop-down-box/2882.html

javascript drop down box www.computing.net/answers/webdevel/javascript-drop-down-box/1437.html

drop down box, onchange envokes javascript www.computing.net/answers/webdevel/drop-down-box-onchange-envokes-javascript/4303.html