tags:

views:

72

answers:

4

Ok here's the deal, I'm trying to have a switch statement redirect to another page depending on what option is selected in a select box. Below is a sample i'm working with and trying to figure out, any help at all would be greatly apreciated.

    <html>
<body>
<form name="form1">
<select name="select1">
<option value="p1">p1</option>
<option value="p2">p2</optino>
</select>
</form>


<script type="text/javascript">
<!--
var sel = document.form1.select1;
var txt = sel.options[sel.selectedIndex].text;
var opt = sel.options[sel.selectedIndex].value;

switch (sel)
{
  case 'p1': window.location = 'http://www.yahoo.com'
            break;
  case 'p2': window.location = 'http://www.google..com'
            break;
}



</script>

<p>Set the variable to different value and then try...</p>
</body>
</html>
+5  A: 

I recommend you avoid switch-statements. How about this?

<select name="select1">
  <option data-url="http://www.yahoo.com" value="p1">p1</option>
  <option data-url="http://www.google.com" value="p2">p2</option>
</select>

And:

document.getElementById("select1").onchange = function () {
    var url = this.options[this.selectedIndex].getAttribute("data-url");
    window.location = url;
};

Edit: I changed the example to use html 5 data attributes (not to worry, they are 100% supported in all browsers), since you need the value attribute for something else.

Magnar
the thing is that I need the value to equal something specific as it is being dropped into a mysql database, thank you though.
NewB
This could be done by modifying the data URL to be `page.php?value=CONTENTS_OF_VALUE_FIELD_HERE` to pass a GET variable
Nico Burns
oops, also forgot to mention that the change needs to happen when clicking on a submit button
NewB
If your plan is to submit something to your server, then using window.location won't work with that. It will interrupt your form posting. How about the server redirect to the correct URL instead of doing it on the client?
Magnar
I'm not sure if doing it from there server is the way to go since this is part of a multi page php wizard where the data only gets posted to the database at the very last step and the select box is towards the beggining. Thank you to all of you for providing me with these answers. Is there another method using php to redirect based on the option selected in a select box?
NewB
+1  A: 

i noticed that you did not bind an change handler to the select, so when the user selects a value nothing happens.

mkoryak
+1  A: 

You've got some typos, your second "/option" and your "google.com". And the switch statement has nothing to do with your error. You have no event listener/onchange handler and you aren't switching with the value from the select option

https://developer.mozilla.org/en/DOM/element.onchange https://developer.mozilla.org/en/DOM/element.addEventListener

wybiral
+1  A: 
  1. You need to bind an event handler
  2. Your switch was using the wrong value

See http://www.jsfiddle.net/vDFpZ/

<html>
<body>
<form name="form1">
<select name="select1">
<option value="p1">p1</option>
<option value="p2">p2</option>
</select>
</form>


<script type="text/javascript">
    window.onload = function () {
        document.getElementsByName("select1")[0].onchange = function () {
            var sel = this.options[this.selectedIndex];
            var text = sel.text;
            var val = sel.value;

            switch (val)
            {
              case 'p1': window.location = 'http://www.yahoo.com'
                        break;
              case 'p2': window.location = 'http://www.google..com'
                        break;
            }
        }
    }
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>​
Matt
the example above by Matt seems to be working great but I need it linked to a submit button instead of loading when the selection occurs. I'm a total javascript newb and I admit I have a lot of reading and practice to do but I need this one aspect for a project that needs to be completed very soon.
NewB