Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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 Attached Files

  • #2
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    This change should do it.
    Code:
    writeCookie("user_city", userCity.value, 5 * 365);

  • Users who have thanked RandomUser531 for this post:

    thejackkelly (07-18-2010)

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That did the trick! (along with adjusting the form "values" to appropriate names).

    Thanks for the help.

    Best wishes,
    Jack

  • #4
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    Jul 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excellent...thank you sooooo much have a great day!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •