Very easy. I was even going to suggest that before as an alternative lol.
When the user makes a selection in the select box, and onChange action is thrown. We can assign a function to be run when that event occurs using JavaScipt.
First we'll write our function:
function surfTo(sURL) {
window.open(sURL,"main","");
}
All it does is just open the URL that is passed in as sURL in a new window.
All we need to do then is just assign this function to the select box:
print "<select name=\"sPageURL\" onChange=\"surfTo(this.options[this.selectedIndex].value)\">\n";
// Then we use a loop to create the menu
while($row = mysql_fetch_array($query))
{
print "<option value=\"profile.php?name=$row[value]\">$row[name]</option>\n";
}
print "</select>\n";
So basically we assign the function to the event like this:
onChange="surfTo(this.options[this.selectedIndex].value"
'this' is a reference to that element.
options is an array referencing each of the <option> tags for that select box.
We use this.selectedIndex to get an index into the options array for the currently selected option. this.options[this.selectedIndex] will return a Option element. So you then need to use value property to get the actual value from the element. (the url)
So, that should work accross all browsers. Some people would just write this.selectedValue to get the selected value from the box. That will only work in IE, so you always need to be carefull when writing JavaScript code... make sure you test it on multiple browsers.
cya man
-Adam 🙂