Forum Moderators: open

Message Too Old, No Replies

Handling "multiple" select box in form submission

how do you do it?

         

txbakers

5:23 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When submitting an HTML form, the name of the select box is equal to the selectedIndex.value in a querystring. For example:

mypage.asp?list=Sam

Sam was selected as the item from the drop down box.

However, when multiple is allowed in the select the resulting querystring looks like this:

mypage.asp?list=Sam&list=Dave&list=Sue

This is not very good for submitting data or working with queries at all.

I was able to write some javascript to intercept the form submission and use a "location.href=" instead of a submit, but that's not very reliable. My new querystring looked like this:

mypage.asp?list=Sam¦Dave¦Sue

which is fine to use. I can parse it in my destination page and do something with the different parts of the data.

How do other people handle the "multiple" in a form submission? (Other than using checkboxes - I already do that in one application)

dingman

6:25 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if the same trick works in ASP, but in PHP you can declare the form variable as an array by adding '[]' to the variable name in the form, and PHP will then initialize it as an array containing the selectecd values. ie,

<select multiple="" name="list[]">
... list options here ...
</select>

Like I said, I don't know if this works with other scripting languages, but it seems reasonable.

txbakers

7:01 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think HTML likes that.

my resulting querystring was:

b.asp?list%5B%5D=dave&list%5B%5D=red

the %5B and %5D were the [] characters escaped for URL.

dingman

7:20 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm... pulled that one from a book a while ago 'cause I was curious, but haven't needed to apply it myself yet. I'm not suprised that the browser would escape the brackets, but I kinda figured the language engine would decod them again. It usually does stuff like that transparently. (Which is wonderful except when it's frustrating :))

SuzyUK

9:16 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



txbakers

I'm a relative newbie to asp, but this querystring produces an array, could you Dim the request, then use the split function (using the comma as seperator) to produce a list, then giving each item it's own variable name for use in your next query?

Suzy

andreasfriedrich

9:33 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dingman,

the same is explained in the PHP documentation in the section on Variables from outside PHP [php.net] as well. It works great and I have been using it for years now.

Andreas

andreasfriedrich

9:46 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



txbakers

all in all I don´t think it is a good idea to mess around with the way data are submitted by the browser. If you don´t like the long query string you should post the data.

I´m sure ASP has a way to handle multiple selects and store them in an array just as PHP or Lincoln Stein´s CGI.pm has.

Andreas

SuzyUK

10:00 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi txbakers...

I found this works, then the values can be used in an SQL insert statement

I created a mypage.asp with the code below, then visited it using your query string

<%
'-- this first line just shows how the querystring
'-- is being read and is for information only
response.write Request.Querystring("list") & "<br /><br />"

'-- dim the querystring to a variable
Dim varArray
varArray = request.querystring("list")

'-- split the array, using the comma as delimiter
varItems = split(varArray, ",")

'-- loop through the list
For Each Item in varItems
value = trim(item)
Response.Write value & "<br />"

'-- hopefully you are getting the values,
'-- and can now change the response.write to an sql insert statement
NEXT
%>

HTH
Suzy

txbakers

10:02 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not a question of a long query string. It's a question of how the variables are sent. With a multiple select the name of the variable appears once for each time there is a selected option:

name=fred&name=harv&name=sue&name=bill

The Request.QueryString("name") would freak out at that.

I want to send the data in the form as name=fred¦harv¦sue
bill

I suppose I could try to do it by reading the entire querystring in the receiving page, but that would be a much trickier proposition than what I'm doing now.

I'll post the question on an ASP board and see what shakes out.

txbakers

10:04 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Suzy, I like that one. I'll give it a whirl and see if I can make it work.

SuzyUK

10:18 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is there an ASP forum here? and if so what number..

anyhow if that's the way (name = sam¦dave¦sue) you want to send the data it's even easier

<%
'-- dim the querystring to a variable
Dim varArray
varArray = request.querystring("list")

'--this will produce the variable you're looking for
name = Replace(varArray, "," , "¦")

'-- just to test
response.write name
%>

Suzy

andreasfriedrich

10:30 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I still don´t think messing around with form submission is a good idea especially since I don´t see the need for it.

The Request.QueryString("name") would freak out at that.

Why would it? Have a look at Microsoft´s documentation of the Request.QueryString Collection [msdn.microsoft.com].

The value of Request.QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING.

Andreas

txbakers

11:16 pm on Oct 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if so, that would be fabulous! I'll check it out. Simplest solution is always the best.

txbakers

1:19 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



works like it wasn't made by Microsoft! Thanks for the help.