Welcome to WebmasterWorld Guest from 18.212.222.217

Forum Moderators: open

Message Too Old, No Replies

Multiple select drop down box without Javascript

     
3:26 pm on May 24, 2004 (gmt 0)

New User

10+ Year Member

joined:Feb 26, 2004
posts:26
votes: 0


Hello,

I wonder if it is possible to have a drop down box where I can select more than one value without using Javascript. For example is it possible to have check boxes within a drop down box? How?

Thanks,
Jacco

3:39 pm on May 24, 2004 (gmt 0)

New User

10+ Year Member

joined:May 24, 2004
posts:10
votes: 0


Checkboxes within a dropdown box? Nuh uh, not that I know of.

To select more than one value: shift + click OR ctrl + click

5:15 pm on May 24, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


There are several ways to do that using Javascript/DHTML, but none using standard HTML form syntax.

If it's a drop-down form element...it's one selection only. If you use the <select multiple...> syntax, which allows the user to select more than one item from the list, it becomes a scrolling list. That's when Ctrl+Click and Shift+Click come into play.

Using Javascript to create hidden, positioned layers containing a drop-down form element simulation with added checkboxes that select item(s) from the fake drop-down is one method you might consider, if Javascript becomes an option.

7:26 pm on May 24, 2004 (gmt 0)

New User

10+ Year Member

joined:May 24, 2004
posts:8
votes: 0


Not sure about multiple select dropdowns, but I know for a list box, you would put "multiple" in the select tag..ex: <select name="lstBlah" multiple>
7:38 pm on May 24, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


If it's a drop-down form element...it's one selection only. If you use the <select multiple...> syntax, which allows the user to select more than one item from the list, it becomes a scrolling list. That's when Ctrl+Click and Shift+Click come into play.

Not sure about multiple select dropdowns, but I know for a list box, you would put "multiple" in the select tag..ex: <select name="lstBlah" multiple>

Precisely! Then hold the control button on your keyboard to select as many as you want. If you care to keep the code up to date use:

<select name="somename" multiple="multiple">

And, if I'm not mistaken, you need to add an empty set of brackets to the name of the select:

<select name="somename[]" multiple="multiple">

Without the brackets you will only get the value of the last selected option. The brackets force the value to be an array, which can be accessed by:

foreach($_POST['somename'] as $val) {
print $val;
}

11:51 pm on May 24, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


Standard ol' HTML syntax:

(The default <option> value is the text next to it. Indicate an explicit value if you want it to be something different, i.e. <option value="one">1 )

<select name="list1">
<option>1
<option>2
<option>3
</select>

Produces a single-line, drop-down form element from which only one item may be selected. When the form is posted, the element sends its data as "list1=2", or whatever the choice was.

<select name="list1" multiple>
<option>1
<option>2
<option>3
</select>

Produces a four-line (default for "multiple" types) scrolling (if req.) element from which more than one item may be selected, using the Shift and Ctrl keys as we all know how to. For each item selected from this element, a separate variable=value will be posted. If I select 1 and 3, then my form would post "list1=1&list1=3".

<select name="list1" multiple size=1>
<option>1
<option>2
<option>3
</select>

Produces a single-line form element with tiny scrollbars that behaves exactly as my second example, with regard to posting variables and values.

Using an array indicator to attempt to produce an ordered array for multiple selections in this type of form does not do that. With this type of element, there is no automatic array-generation. You may, however, infer an array from multiple element name entries in the posted data. (i.e. loop through variables and form arrays from multiple entries with the same name).

You MAY use the array indicator with checkbox form elements to create an array on posting. i.e.

<input type="checkbox" name="box[]" value=1>1
<input type="checkbox" name="box[]" value=2>2
<input type="checkbox" name="box[]" value=3>3

Will result in three variables being sent, each named "box[]". The "box[]" variable will be an array, however there will be no ordering visible by default.

4:45 pm on May 25, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


I need to correct myself, after a little experiment.

I said, "Using an array indicator to attempt to produce an ordered array for multiple selections in this type of form [select] does not do that."

Similarly to checkboxes that use an array indicator, form elements of the SELECT type, in the presence of the MULTIPLE paramter and when NAMEd using an array indicator (i.e. list[]) produces the same result as the CHECKBOX-as-array illustration, above.

Like the CHECKBOX array, variables and values passed using a SELECT array can be accessed AS an array on posting.

Sorry 'bout that. And good idea, Birdman! :)

2:21 pm on May 27, 2004 (gmt 0)

New User

10+ Year Member

joined:Feb 26, 2004
posts:26
votes: 0


Thank you very much for all your replies! So what I assumed is true. It's not possbible :) Well, as you pointed out StupidScript, I can use a single line form element. That would be a good alternative I think. But only if it would be possible to have an input field within this element. Is this possible? Because the user has to give a value for the selected element. If not I will take a complete other approach... :)

Thanks!

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members