Connecting Tech Pros Worldwide Forums | Help | Site Map

Get multiple select values from query string?

johkar
Guest
 
Posts: n/a
#1: Jul 20 '05
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&status=red

function getParams() {
var index = document.URL.indexOf('?');
var params = new Array();
if ( index != -1 ) {
var nameValuePairs
=document.URL.substring(index+1,document.URL.lengt h).split('&');
for ( var i=0; i<nameValuePairs.length; i++ ) {
nameVal = nameValuePairs[i].split('=');
params[nameVal[0]] = nameVal[1];

}

} return params;
}

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

John



Oz
Guest
 
Posts: n/a
#2: Jul 20 '05

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<nameValuePairs.length; i++ ) {
> nameVal = nameValuePairs[i].split('=');
> params[nameVal[0]] = nameVal[1];
>
> }[/color]

To:

for ( var i=0; i<nameValuePairs.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@link.net> wrote in message
news:0e_lb.3492$wc3.1100@newsread3.news.pas.earthl ink.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&status=red
>
> function getParams() {
> var index = document.URL.indexOf('?');
> var params = new Array();
> if ( index != -1 ) {
> var nameValuePairs
> =document.URL.substring(index+1,document.URL.lengt h).split('&');
> for ( var i=0; i<nameValuePairs.length; i++ ) {
> nameVal = nameValuePairs[i].split('=');
> params[nameVal[0]] = nameVal[1];
>
> }
>
> } return params;
> }
>
> var mystat=unescape(params["status"]);
>
> John
>
>[/color]


johkar
Guest
 
Posts: n/a
#3: Jul 20 '05

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:kcKdnfTd2aRo4gSiRVn-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<nameValuePairs.length; i++ ) {
> > nameVal = nameValuePairs[i].split('=');
> > params[nameVal[0]] = nameVal[1];
> >
> > }[/color]
>
> To:
>
> for ( var i=0; i<nameValuePairs.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



Mike
Guest
 
Posts: n/a
#4: Jul 20 '05

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(new Object());

For complete documentation check out

http://devedge.netscape.com/library/...nce/array.html
http://devedge.netscape.com/library/.../contents.html




"johkar" <nosendjunk@link.net> wrote in message
news:jtumb.1149$Px2.846@newsread4.news.pas.earthli nk.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:kcKdnfTd2aRo4gSiRVn-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<nameValuePairs.length; i++ ) {
> > > nameVal = nameValuePairs[i].split('=');
> > > params[nameVal[0]] = nameVal[1];
> > >
> > > }[/color]
> >
> > To:
> >
> > for ( var i=0; i<nameValuePairs.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]


johkar
Guest
 
Posts: n/a
#5: Jul 20 '05

re: Get multiple select values from query string?


Thanks


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Jul 20 '05

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
Closed Thread