SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code javascript:
    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:
    HTML Code:
      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:
    Code javascript:
      tempStr += "city="+document.form1.city.value+"&";

    HTML:
    HTML Code:
    <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:

    Code:
    http://link.somedomain.com/somedirectory/begin&MyValuetype=Type2&city=City1,City3,City5,City7
    However I get This:
    Code:
    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!!!!!
    Last edited by ScallioXTX; Nov 12, 2010 at 12:29. Reason: added syntax highlighting

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try this

    Code JavaScript:
    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;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •