This has to be easy....Right?

Hi All,

I am trying to add a script that produces a link with values and I am having trouble getting a multi select field to produce the correct URL output.

For example:


function evalURL() {
  var tempStr = "";
  tempStr="http://link.somedomain.com/somedirectory/begin&"+document.form1.value+"&";
  tempStr += "MyValuetype="+document.form1.MyValuetype.value+"&";
  tempStr += "city="+document.form1.city.value+"&";  
  document.location=tempStr;
}

The HTML Looks Like This:


  Select Type: 
 <select name="MyValuetype">
    <option value="Type1">Type 1</option>
    <option value="Type2">Type 2</option>
    <option value="Type3">Type 3</option>
    <option value="Type4">Type 4</option>
  </select> (REQUIRED)</font><p>
  <font face="Arial">
  <br />

That all works ok, however the string above for the city works if my html only selects one city. But I need to Select Multiple cities and have the output reflect that

String:


  tempStr += "city="+document.form1.city.value+"&";  

HTML:


<font face="Arial">
Select City:
<select name="city" multiple="multiple" size="5"> 
<option selected="selected" value="">All</option>
<option value="City1,">City 1</option>
<option value="City2,">City 2</option>
<option value="City3,">City 3</option>
<option value="City4,">City 4</option>
<option value="City5,">City 5</option>
<option value="City6,">City 6</option>
<option value="City7,">City 7</option>
</select>

If I select Type 2 for the first selection and Multiple Cities, City 1, 3, 5 & 7 the out put link when I press the submit button should look like this:


http://link.somedomain.com/somedirectory/begin&MyValuetype=Type2&city=City1,City3,City5,City7

However I get This:


http://link.somedomain.com/somedirectory/begin&MyValuetype=Type2&city=City1

How do I adjust the String to Allow for multiple selections separated by a comma?

And another question I had was How do I omit the field all together if it is not selected. If the Form field is any or all or not required by the user it should not be added to the string the output contains the data request but the value is either blank or whatever I tell it, but it messes up the output sting.

If I did a poor job explaining this, please let me know I will try again.

Thank You ALL in advance!!!

Try this

function evalURL() {
    var tempStr = "", cityStr = "";
    var ob = document.form1.city;
    
    for(var i=0, max=ob.length; i<max; i++) {
        if (ob.options[i].selected) {
            if (i > 0) cityStr += ", ";
            cityStr += ob.options[i].value;
        }
    }
    
    tempStr="http://link.somedomain.com/somedirectory/begin&"+document.form1.value+"&";
    tempStr += "MyValuetype="+document.form1.MyValuetype.value+"&";
    tempStr += "city="+cityStr+"&"; 
    document.location=tempStr;
}