Multiple option selecting in HTML forms

Philip Greenspun's Homepage : Philip Greenspun's Homepage Discussion Forums : 6916 : One Thread
Notify me of new responses
I'm trying to get multiple options selected from the
<select multiple name=myselect> tag of an HTML form to be
sent through to the next tcl page.

I only seem to be getting the first value selected in
$myselect however. Is this something that I just have to
deal with, no multiple selects?

It doesn't seem to be coming through as an array of values
or anything that would be useful, just a single value.
Help would be appreciated.

-- Christopher Bachmann, November 11, 1999

Answers

Clarification

Actually, it seems to give me the LAST value, so I get the impression that with each option selected it sees it writes over the $myselect value. (?)

-- Christopher Bachmann, November 11, 1999

If you read the source code for set_form_variables, it will be easy for you to figure out how to get all the values of myselect.

-- Eve Andersson, November 11, 1999

set_the_usual_form_variables doesn't handle multiple selects, checkboxes, and other multiple value single var name form constructs very well.

try doing something like this (where var_name is the name of your form input):

and then iterate through the list (var_name_list) to do whatever you want.



-- Yonatan Feldman, November 11, 1999

Here is a version of set_the_usual_form_variables that will separate multiple values of the same variable with ";".
# tea- modified to handle multiple select and checkboxes correctly
# values will be separated by ";"

# Note: it is impossible to put mulipleselects in a list 
# and guarantee it the value will be a list using this function
# because there is no way to tell if a field with one selected
# value is a multiple select or not.



proc set_the_usual_form_variables {{error_if_not_found_p 1}} {

    if { $error_if_not_found_p == 1} {
	uplevel { if { [ns_getform] == "" } {
	    ns_returnerror 500 "Missing form data"
	    return
	}
        }
     } else {
	 uplevel { if { [ns_getform] == "" } {
	     # we're not supposed to barf at the user but we want to return
	     # from this subroutine anyway because otherwise we'd get an error
	     return
	 }
         }
     }   
 
     uplevel {
	 set form [ns_getform] 
	 set form_size [ns_set size $form]
	 set form_counter_i 0

	 while { $form_counter_i<$form_size } {

	     if { ![info exists QQ[ns_set key $form $form_counter_i]] } { 
		 set [ns_set key $form $form_counter_i] [string trim [ns_set value $form $form_counter_i]]
		 set QQ[ns_set key $form $form_counter_i] [DoubleApos [string trim [ns_set value $form $form_counter_i]]]
	     } else {
		 append  [ns_set key $form $form_counter_i] ";[ns_set value $form $form_counter_i]"
		append  QQ[ns_set key $form $form_counter_i] ";[DoubleApos [ns_set value $form $form_counter_i]]"
	    }
	    
	    incr form_counter_i
	}
    }
}


-- Tracy Adams, November 11, 1999

Alternatively, since multiple select lists and checkboxes come through exactly the same in form variables, just use the existing procedure util_GetCheckboxValues.

-- Kevin Scaldeferri, December 10, 1999

When you create your HTML form, follow the name of the element with a set of square brackets '[]'. This will return an array with each of the selected options...

You can then find this array through what-ever scripting language you are using.. e.g. PHP: $_POST['your_multiple_option_field_name'][key]

-- Noel da Costa, May 4, 2004


Im having the same problem, Im using the GET method and it seems the url sting is encoded with the select in the following manner.

name=option+name=option+name=option+name=

and so only the last value which is null is shown when I use php's $_GET[name]

I had to resort to regular expressions to take out the values I needed but I would like to know the right way of handling multiple select options.

Thanx.

-- gagee egegs, August 31, 2004


If you want ot use multiple optin selection in HTML try using an array instead for the name field: <select multiple name=myselect[]>

if you use php access like this: $myselect = $HTTP_POST_VARS['myselect[]'];

foreach($myslect as $select) { echo 'My select: '.$select; }

Good luck!

-- Chris Kunze, August 25, 2005