Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-17-2010, 11:33 PM   PM User | #1
thejackkelly
New Coder

 
Join Date: Jul 2010
Posts: 16
Thanks: 4
Thanked 0 Times in 0 Posts
thejackkelly is an unknown quantity at this point
Use form input to write cookie for redirection

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):

Code:
<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");
  }

else 
 {
 window.location.replace("index.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); 

}

</script>

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

</head>

<body>

		<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>
			</select> 


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

		</form>

</body>
</html>
Attached Files
File Type: doc cookie.doc (27.5 KB, 68 views)
thejackkelly is offline   Reply With Quote
Old 07-18-2010, 12:10 AM   PM User | #2
RandomUser531
New Coder

 
Join Date: Jul 2010
Posts: 61
Thanks: 0
Thanked 21 Times in 21 Posts
RandomUser531 is on a distinguished road
This change should do it.
Code:
writeCookie("user_city", userCity.value, 5 * 365);
RandomUser531 is offline   Reply With Quote
Users who have thanked RandomUser531 for this post:
thejackkelly (07-18-2010)
Old 07-18-2010, 12:24 AM   PM User | #3
thejackkelly
New Coder

 
Join Date: Jul 2010
Posts: 16
Thanks: 4
Thanked 0 Times in 0 Posts
thejackkelly is an unknown quantity at this point
That did the trick! (along with adjusting the form "values" to appropriate names).

Thanks for the help.

Best wishes,
Jack
thejackkelly is offline   Reply With Quote
Old 03-30-2011, 04:27 PM   PM User | #4
mikeygunz
New to the CF scene

 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mikeygunz is an unknown quantity at this point
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.

Thanks!
Mike
mikeygunz is offline   Reply With Quote
Old 03-30-2011, 05:02 PM   PM User | #5
thejackkelly
New Coder

 
Join Date: Jul 2010
Posts: 16
Thanks: 4
Thanked 0 Times in 0 Posts
thejackkelly is an unknown quantity at this point
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

Last edited by thejackkelly; 03-30-2011 at 05:05 PM..
thejackkelly is offline   Reply With Quote
Old 03-30-2011, 06:11 PM   PM User | #6
mikeygunz
New to the CF scene

 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mikeygunz is an unknown quantity at this point
Excellent...thank you sooooo much have a great day!
mikeygunz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:41 PM.


Advertisement
Log in to turn off these ads.