View Full Version : Use form input to write cookie for redirection

07-18-2010, 12:33 AM
Hello. My goal is for the user's input on their first visit to redirect their landing page in the future. I am a novice and any help is very much appreciated.

The form's drop down menu includes three cities (Calgary, Toronto, Vancouver). When the user presses "submit form", the script should create a cookie to store the user's city selection. Next time the user visits the site, they should be redirected to their city's site, ie. "/vancouver.html"

Right now, the code is returning an error from the onload("redirect();") function. The error indicates I am trying to redirect to "/[object HTMLSelectElement].html", but I am not sure if the trouble is in reading or writing the cookie. For simplicity, I've removed most of the other content from this page (my cookie.js is attached as a .doc):

<title>Site Title</title>

<script type="text/javascript" src="cookie.js"></script>

<script language="JavaScript" type="text/javascript">

function redirect() {

userCity = readCookie ("user_city");

if (userCity)
window.location.replace(userCity + ".html");



function setCity(box) {

var userCity =(document.welcomeSignUp.userCity);
for (i=0; i<userCity.length; i++) {
if (userCity[i].name != box.name) {
userCity[i].selectedIndex = 0;

writeCookie("user_city", userCity, 5 * 365);



<link type="text/css" rel="stylesheet" href="style.css" />



<form id="welcomeSignUp" action="" method="get" name="welcomeSignUp" onload="redirect();">

Your Email address: <input type="text" name="email" value="" size ="20" />
<br />

Choose Your City: <select name="userCity" size="20">
<option value="one">Calgary</option>
<option value="two">Toronto</option>
<option value="three">Vancouver</option>

<a href="index.html"><button class="rounded" onclick="setCity(this);">
<span>submit form</span>



07-18-2010, 01:10 AM
This change should do it.

writeCookie("user_city", userCity.value, 5 * 365);

07-18-2010, 01:24 AM
That did the trick! (along with adjusting the form "values" to appropriate names).

Thanks for the help.

Best wishes,

03-30-2011, 05:27 PM
Hi Jack - I'm trying to do the exact same thing except change the city names...would you be able to post the finished code and all of the files associated with it. I'm a newbie too and was having problems getting the above code to work.


03-30-2011, 06:02 PM
Attached. It is a little bit chaotic, but you should find the code you need at the top of index.html

Good luck!

EDIT: file size was too large, get it here: www.get.oneclickmail.com/Archive.zip

03-30-2011, 07:11 PM
Excellent...thank you sooooo much have a great day!