473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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&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
Jul 20 '05 #1
5 14626
Oz
Instead of storing just the value, store the value as an Array. That way a
single param can have multiple values.

change:
for ( var i=0; i<nameValuePairs.length; i++ ) {
nameVal = nameValuePairs[i].split('=');
params[nameVal[0]] = nameVal[1];

}
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" <no********@link.net> wrote in message
news:0e*****************@newsread3.news.pas.earthl ink.net... 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

Jul 20 '05 #2
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:kc********************@comcast.com...
Instead of storing just the value, store the value as an Array. That way a
single param can have multiple values.

change:
for ( var i=0; i<nameValuePairs.length; i++ ) {
nameVal = nameValuePairs[i].split('=');
params[nameVal[0]] = nameVal[1];

}


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]);
}
}

snip

Jul 20 '05 #3
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
params[nameVal[0]] = [nameVal[1]]; this could also be written params[nameVal[0]] = new Array(nameVal[1]);
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" <no********@link.net> wrote in message
news:jt****************@newsread4.news.pas.earthli nk.net...
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:kc********************@comcast.com...
Instead of storing just the value, store the value as an Array. That way

a single param can have multiple values.

change:
for ( var i=0; i<nameValuePairs.length; i++ ) {
nameVal = nameValuePairs[i].split('=');
params[nameVal[0]] = nameVal[1];

}


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]);
}
}

snip

Jul 20 '05 #4
Thanks
Jul 20 '05 #5
Mike wrote:
So the first time the param is encountered a the value was stored in an
array in the following line
> > params[nameVal[0]] = [nameVal[1]]; this could also be written > > params[nameVal[0]] = new Array(nameVal[1]);


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
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
2
by: DC Gringo | last post by:
I have two listboxes, the first of which is an autopostback=true that allows multiple row selection. When I select multiple values (by holding down CTL) in the first one, it should query the...
8
by: Chris A via AccessMonster.com | last post by:
I have an interesting problem that I have yet to come accross that I can't change data structure on because it is an export from filemaker I am reformatting for another dept. anyway. I have a table...
2
by: Diego | last post by:
Hi everybody! I'm using DB2 PE v8.2.3 for linux. I've defined a database with the following schema: ANNOTATION(ID,AUTHOR,TEXT) ANNOTATION_BOOK(ANNOTATION_ID,OBJECT_ID)...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
3
by: scott | last post by:
Hello all, I am ripping my hair out over this and maybe someone could help. I have a site that has groups and subgroups of those groups that all have int id's. I am trying to have a...
1
by: rosie2006 | last post by:
Environment: .net2.0 over oracle Problem? a simple select statement within a DetailsView that gets two parameter values from a GridView SelectedValue. It might be valuable to understand that to...
2
by: englishman69 | last post by:
Hello, I have been banging my head against this one for a while... Searches online have revealed many different proposals for correcting my issue but none that I can follow! My basic situation...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.