469,344 Members | 5,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

opening a new window with a switch case

mcc
3
I am having a problem with my homework I cant seem to get the redirection with onClick. The script is supposed to have the user insert the name and password and the choose a department from a drop down. The instruction state that we have to use a case statement. Below is the code. Iím new here to this forum so please forgive my messiness in the post. Any help will be greatly appreciated THANKS in advance! I know its choppy and I have changes things up so many times I'm sure my mistakes are in the if statement throught the case ......

here is the script:

Expand|Select|Wrap|Line Numbers
  1. <form method = "post" name= "frmlogin" action = "javascript:void(0)"> 
  2.  
  3. User Name<br />
  4. <input type ="text" name ="txtUserName"><br />
  5. <br />
  6. Password<br />
  7. <input type ="text" name ="txtPassword"><br />
  8. <br />
  9. Department</b>
  10. <select name = "1stDepartment">
  11. <option value="employee.htm">Employee</option>
  12. <option value="customer.htm">Customer</option>
  13. <option value="administration.htm">Administration</option>
  14. <option value="finance.htm">Finance</option>
  15. <option value="hr.htm">HR</option>
  16. <option value="it.htm">IT</option>
  17. <option value="managment.htm">Managment</option>
  18. <option value="manufacturing.htm">Manufacturing</option>
  19. <option value="marketing.htm">Marketing</option>
  20. <option value="webmaster.htm">Webmaster</option>
  21. </select><br />
  22. <br />
  23. <input type="button" name= "btnLogin" value="Go to the Department Web Site" onClick ="login()" > 
  24.  
  25. <input type="reset" name= "btnClear" value="Clear Form">
  26.  
  27.  
  28. </form>
  29.  
  30. <script language = "JavaScript">
  31. function login(strUserName){
  32. var strUserName, strPassword, strDepartment;
  33. strUserName = document.frmlogin.txtUserName.value;
  34. strPassword = document.frmlogin.txtPassword.value;
  35. strDepartment = document.frmlogin.value;
  36.  
  37. switch (strUserName){
  38.  case "kFoster":
  39.   if (strPassword = "MST3K"){
  40.     window.location.href= ("strDepartment.htm");
  41.     }
  42.     else{
  43.     location.href= ("loginFormJS.htm");
  44.     }
  45.     break;
  46.  case "CSchawab":
  47.   if (strPassword = "OKC802"){
  48.     location.href= ("strDepartment.htm");
  49.     }
  50.     else{
  51.     location.href= ("loginFormJS.htm");
  52.     }
  53.     break;
  54.  case "FHelms":
  55.   if (strPassword = "telcom52"){
  56.     location.href= ("strDepartment.htm");
  57.     }
  58.     else{
  59.     location.href= ("loginFormJS.htm");
  60.     }
  61.     break;
  62.  case "kShea":
  63.   if (strPassword = "Ghost716"){
  64.     location.href= ("strDepartment.htm");
  65.     }
  66.     else{
  67.     location.href= ("loginFormJS.htm");
  68.     }
  69.     break;
  70.         }
  71.  
  72.  
  73.       }
  74. </script>
Sep 10 '07 #1
5 1409
epots9
1,351 Expert 1GB
Hi mcc welcome to TSDN,

inside your if statements you are using a single equal sign "=", that means your assigning values and it is always true. what you want to do is change
those in the if statement to double equal signs "==", that will test the considtions and return true or false and based on your code it do perform a certain task.
Sep 10 '07 #2
mcc
3
Hi mcc welcome to TSDN,

inside your if statements you are using a single equal sign "=", that means your assigning values and it is always true. what you want to do is change
those in the if statement to double equal signs "==", that will test the considtions and return true or false and based on your code it do perform a certain task.
Thanks I should have known better! Let me give that a try
Thanks again!
Sep 10 '07 #3
mcc
3
Thanks for the welcome...forgot to add that. Anyways I used the == instead of the =. Changed things up.I dont get any errors in IE or FireFox error council, but is still does not direct to the page or any error page. Still stuck!
Thanks
redone
Expand|Select|Wrap|Line Numbers
  1. <form method = "post" name= "frmlogin" action = "javascript:void(0)"> 
  2.  
  3. User Name<br />
  4. <input type ="text" name ="txtUserName"><br />
  5. <br />
  6. Password<br />
  7. <input type ="text" name ="txtPassword"><br />
  8. <br />
  9. Department</b>
  10. <select name = "1stDepartment">
  11. <option value="employee.htm">Employee</option>
  12. <option value="customer.htm">Customer</option>
  13. <option value="administration.htm">Administration</option>
  14. <option value="finance.htm">Finance</option>
  15. <option value="hr.htm">HR</option>
  16. <option value="it.htm">IT</option>
  17. <option value="managment.htm">Managment</option>
  18. <option value="manufacturing.htm">Manufacturing</option>
  19. <option value="marketing.htm">Marketing</option>
  20. <option value="webmaster.htm">Webmaster</option>
  21. </select><br />
  22. <br />
  23. <input type="button" name= "btnLogin" value="Go to the Department Web Site" onClick ="login()" > 
  24.  
  25. <input type="reset" name= "btnClear" value="Clear Form">
  26.  
  27.  
  28. </form>
  29.  
Expand|Select|Wrap|Line Numbers
  1. <script language = "JavaScript">
  2. function login(strUserName){
  3. var strUserName, strPassword, strDepartment;
  4. strUserName = document.frmlogin.txtUserName.value;
  5. strPassword = document.frmlogin.txtPassword.value;
  6. strDepartment = document.frmlogin.value;
  7.  
  8. switch (strUserName){
  9.  case "kFoster":
  10.   if (strPassword == "MST3K"){
  11.     location.href= strDepartment;
  12.     }
  13.     else{
  14.     location.href= "loginFormJS.htm";
  15.     }
  16.     break;
  17.  case "CSchawab":
  18.   if (strPassword == "OKC802"){
  19.     location.href= strDepartment;
  20.     }
  21.     else{
  22.     location.href= "loginFormJS.htm";
  23.     }
  24.     break;
  25.  case "FHelms":
  26.   if (strPassword == "telcom52"){
  27.     location.href= strDepartment;
  28.     }
  29.     else{
  30.     location.href= "loginFormJS.htm";
  31.     }
  32.     break;
  33.  case "kShea":
  34.   if (strPassword == "Ghost716"){
  35.     location.href= strDepartment;
  36.     }
  37.     else{
  38.     location.href= "loginFormJS.htm";
  39.     }
  40.     break;
  41.         }
  42.  
  43.  
  44.       }
  45. </script>
Sep 10 '07 #4
pbmods
5,821 Expert 4TB
Heya, MCC.

JavaScript CODE tags make your JavaScript easier to read:
[CODE=javascript]
JavaScript code goes here.
[/CODE]

Try alert()ing strUserName before your switch to make sure that you're getting a value, and to confirm that the login() function is getting evaluated.
Sep 11 '07 #5
acoder
16,027 Expert Mod 8TB
mcc, on line 2 in your Javascript, remove the strUsername argument. You're not using it anyway.

For the strDepartment, rename the select so that it doesn't start with a number.

Then, access the select element properly like you have with the username and password.
Sep 11 '07 #6

Post your reply

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

By using this site, you agree to our Privacy Policy and Terms of Use.