Get multiple select values from query string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johkar

    #1

    Get multiple select values from query string?

    This script works fine if all form fields have one value, but what do you do
    if one of the form fields was a multiple select?

    Example: status=blue&sta tus=red

    function getParams() {
    var index = document.URL.in dexOf('?');
    var params = new Array();
    if ( index != -1 ) {
    var nameValuePairs
    =document.URL.s ubstring(index+ 1,document.URL. length).split(' &');
    for ( var i=0; i<nameValuePair s.length; i++ ) {
    nameVal = nameValuePairs[i].split('=');
    params[nameVal[0]] = nameVal[1];

    }

    } return params;
    }

    var mystat=unescape (params["status"]);

    John


  • Oz

    #2
    Re: Get multiple select values from query string?

    Instead of storing just the value, store the value as an Array. That way a
    single param can have multiple values.

    change:[color=blue]
    > for ( var i=0; i<nameValuePair s.length; i++ ) {
    > nameVal = nameValuePairs[i].split('=');
    > params[nameVal[0]] = nameVal[1];
    >
    > }[/color]

    To:

    for ( var i=0; i<nameValuePair s.length; i++ ) {
    nameVal = nameValuePairs[i].split('=');
    if (params[nameVal[0]] == null) {//param doesn't exist so create a
    new array
    params[nameVal[0]] = [nameVal[1]];
    }else{ //param already exists so add to the array
    params[nameVal[0]].push(nameVal[1]);
    }
    }





    "johkar" <nosendjunk@lin k.net> wrote in message
    news:0e_lb.3492 $wc3.1100@newsr ead3.news.pas.e arthlink.net...[color=blue]
    > This script works fine if all form fields have one value, but what do you[/color]
    do[color=blue]
    > if one of the form fields was a multiple select?
    >
    > Example: status=blue&sta tus=red
    >
    > function getParams() {
    > var index = document.URL.in dexOf('?');
    > var params = new Array();
    > if ( index != -1 ) {
    > var nameValuePairs
    > =document.URL.s ubstring(index+ 1,document.URL. length).split(' &');
    > for ( var i=0; i<nameValuePair s.length; i++ ) {
    > nameVal = nameValuePairs[i].split('=');
    > params[nameVal[0]] = nameVal[1];
    >
    > }
    >
    > } return params;
    > }
    >
    > var mystat=unescape (params["status"]);
    >
    > John
    >
    >[/color]


    Comment

    • johkar

      #3
      Re: Get multiple select values from query string?

      Thanks for the reply. Where is the new array declared? What is push, and
      what does it do? Support?

      John

      "Oz" <oz@synovic.com > wrote in message
      news:kcKdnfTd2a Ro4gSiRVn-uA@comcast.com. ..[color=blue]
      > Instead of storing just the value, store the value as an Array. That way a
      > single param can have multiple values.
      >
      > change:[color=green]
      > > for ( var i=0; i<nameValuePair s.length; i++ ) {
      > > nameVal = nameValuePairs[i].split('=');
      > > params[nameVal[0]] = nameVal[1];
      > >
      > > }[/color]
      >
      > To:
      >
      > for ( var i=0; i<nameValuePair s.length; i++ ) {
      > nameVal = nameValuePairs[i].split('=');
      > if (params[nameVal[0]] == null) {//param doesn't exist so create a
      > new array
      > params[nameVal[0]] = [nameVal[1]];
      > }else{ //param already exists so add to the array
      > params[nameVal[0]].push(nameVal[1]);
      > }
      > }[/color]
      snip



      Comment

      • Mike

        #4
        Re: Get multiple select values from query string?

        Arrays can be declared a couple of ways

        var array = new Array();
        or
        var array = [ ]; //just short hand

        So the first time the param is encountered a the value was stored in an
        array in the following line[color=blue][color=green]
        > > params[nameVal[0]] = [nameVal[1]];[/color][/color]
        this could also be written[color=blue][color=green]
        > > params[nameVal[0]] = new Array(nameVal[1]);[/color][/color]

        push is a method on Array objects that automatically adds a new entry to the
        end of an array. so instead of increasing the number of
        members in an array by using:

        myArray[myArray.length] = new Object();

        you can simply use:

        myArray.push(ne w Object());

        For complete documentation check out







        "johkar" <nosendjunk@lin k.net> wrote in message
        news:jtumb.1149 $Px2.846@newsre ad4.news.pas.ea rthlink.net...[color=blue]
        > Thanks for the reply. Where is the new array declared? What is push, and
        > what does it do? Support?
        >
        > John
        >
        > "Oz" <oz@synovic.com > wrote in message
        > news:kcKdnfTd2a Ro4gSiRVn-uA@comcast.com. ..[color=green]
        > > Instead of storing just the value, store the value as an Array. That way[/color][/color]
        a[color=blue][color=green]
        > > single param can have multiple values.
        > >
        > > change:[color=darkred]
        > > > for ( var i=0; i<nameValuePair s.length; i++ ) {
        > > > nameVal = nameValuePairs[i].split('=');
        > > > params[nameVal[0]] = nameVal[1];
        > > >
        > > > }[/color]
        > >
        > > To:
        > >
        > > for ( var i=0; i<nameValuePair s.length; i++ ) {
        > > nameVal = nameValuePairs[i].split('=');
        > > if (params[nameVal[0]] == null) {//param doesn't exist so create[/color][/color]
        a[color=blue][color=green]
        > > new array
        > > params[nameVal[0]] = [nameVal[1]];
        > > }else{ //param already exists so add to the array
        > > params[nameVal[0]].push(nameVal[1]);
        > > }
        > > }[/color]
        > snip
        >
        >
        >[/color]


        Comment

        • johkar

          #5
          Re: Get multiple select values from query string?

          Thanks


          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Get multiple select values from query string?

            Mike wrote:
            [color=blue]
            > So the first time the param is encountered a the value was stored in an
            > array in the following line[color=green][color=darkred]
            > > > params[nameVal[0]] = [nameVal[1]];[/color][/color]
            > this could also be written[color=green][color=darkred]
            > > > params[nameVal[0]] = new Array(nameVal[1]);[/color][/color][/color]

            Depends on the implementation of ECMAScript and the value of nameVal[1].
            The Array constructor function also takes the number of array elements
            as first argument in some implementations . So the Array literal is the
            saner way here.

            BTW Oz, johkar, Mike: You are top-posting, please read
            http://www.netmeister.org/news/learn2quote.html and stop
            that.


            PointedEars

            Comment

            Working...