Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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

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

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>

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.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |