By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

select function to take users to another location

P: 89
Hi all

I have a problem with a form script.

The form script should do the following:

When a user selects a value from a drop down list and hits the "next" button it should take him to a specific site. Also he has the option to check a checkbox "Remeber this choice" so that when he returns to the site it remembers what he has choosen from the drop down list and automatically takes him to a specific site.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. <!--
  3. function newCookie(name,value,days) {
  4.  var days = 100;   // the number at the left reflects the number of days for the cookie to last
  5.                  // modify it according to your needs
  6.  if (days) {
  7.    var date = new Date();
  8.    date.setTime(date.getTime()+(days*24*60*60*1000));
  9.    var expires = "; expires="+date.toGMTString(); }
  10.    else var expires = "";
  11.    document.cookie = name+"="+value+expires+"; path=/"; }
  12.  
  13. function readCookie(name) {
  14.    var nameSG = name + "=";
  15.    var nuller = '';
  16.   if (document.cookie.indexOf(nameSG) == -1)
  17.     return nuller;
  18.  
  19.    var ca = document.cookie.split(';');
  20.   for(var i=0; i<ca.length; i++) {
  21.     var c = ca[i];
  22.     while (c.charAt(0)==' ') c = c.substring(1,c.length);
  23.   if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length); }
  24.     return null; }
  25.  
  26. function eraseCookie(name) {
  27.   newCookie(name,"",1); }
  28.  
  29. function toMem(a) {
  30.     newCookie('theName', document.form.name.value);     // add a new cookie as shown at left for every
  31.     //newCookie('theEmail', document.form.email.value);   // field you wish to have the script remember
  32. }
  33.  
  34. function delMem(a) {
  35.   eraseCookie('theName');   // make sure to add the eraseCookie function for every field
  36.   //eraseCookie('theEmail');
  37.  
  38.    document.form.name.value = '';   // add a line for every field
  39.    }
  40.  
  41. //-->
  42. </script>
  43. <script type="text/javascript">
  44. <!-- 
  45. function goto_URL(object) {
  46. window.location.href = object.options[object.selectedIndex].value; }
  47. //-->
  48. </script>
[HTML]<form name="form" action="http://www.printline-webshop.eu/" method="post" onSubmit="if (this.checker.checked) toMem(this); goto_URL(this.form.name)">
<select name="name" class="" id="name">
<option selected="selected" value="">Select one</option>
<option value="de/index.php">Germany - German</option>
<option value="uk/index.php">Other - English</option>
<option value="se/index.php">Sweden - Swedish</option>
<option value="index.php">Denmark - Dalish</option>
</select>
<input type="submit" value="Next!" class="button" />
<input type="checkbox" id="checker" name="checker" /> <b>Remember this choice</b>
</form>[/HTML]
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2. <!--
  3. document.form.name.value = readCookie("theName");  // Change the names of the fields at right to match the ones in your form.
  4. //document.form.email.value = readCookie("theEmail");
  5. //-->
  6. </script>
The "Remember this choice" works partly. It remebers the choice that was made when returning to the page, but doesn't redirect when entering the site.

When hitting the "next" button it just takes the user to the action url and not the url specified in the option value of the select list.

Can anyone help me getting this script to work correctly...???

Thanks very much in advance!
Sep 30 '08 #1
Share this Question
Share on Google+
2 Replies


rnd me
Expert 100+
P: 427
fixes:


Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2. <!--
  3. var fo = document.form.name
  4. var val = readCookie("theName"); 
  5. if(val){ fo.selectedIndex=val; fo.form.submit(); }
  6.  
  7.  
  8. //-->
  9. </script>

Expand|Select|Wrap|Line Numbers
  1. function toMem(a) {
  2.     newCookie('theName', document.form.name.selectedIndex);   
  3. }
  4.  
  5.  
  6.  
Sep 30 '08 #2

P: 89
Hi' rnd me

I'm having some difficulties getting your fixes to work.

No matter where i put the script:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript"> 
  2. <!-- 
  3. var fo = document.form.name 
  4. var val = readCookie("theName");  
  5. if(val){ fo.selectedIndex=val; fo.form.submit(); } 
  6.  
  7.  
  8. //--> 
  9. </script> 
i get the error that 'document.form.name' is null or not an object.

Also it doesn't remember which value i select in the drop down menu, and it doesn't redirect the user to the specified url in the value field....

Any suggestions to what might the problem...????

Thanks in advance.
Oct 1 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.