Topic: Multi-selection form elements Pages that link to <a href="https://ozoneasylum.com/backlink?for=9666" title="Pages that link to Topic: Multi-selection form elements" rel="nofollow" >Topic: Multi-selection form elements\

 
Author Thread
Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

IP logged posted posted 07-24-2003 13:18 Edit Quote

OK...If I create a form in a HTML page, and I put a SELECT element on it like this:

<SELECT multiple name="x">

when you select multiple parts, how are the seperate selections stored? When I exho them out in the PHP script I have processing the form I only get the last one in the list that was selected. Does it store it in a javascript array or something?

wisenut
Obsessive-Compulsive (I) Inmate

From: somewhere out there
Insane since: Aug 2003

IP logged posted posted 08-06-2003 12:02 Edit Quote

Hi Skaarjj

The various elements that are selected by a user will be separated by commas (e.g. "option1,option2,option3,option4"). So you need to use a function or piece of script to separate the items that have been chosen... OR use one of the in-built functions that are in languages like ASP and PHP to separate the elements chosen into an array.

Bear in mind, however, that if you have text elements in your select list, then you may in a worst-case scenario already have a comma in any of the items that are listed. So the easiest way to separate the chosen items is to store VALUEs against the various items, and read those back into your PHP script, as follows (ignore the spelling which is purposefully wrong for the sake of the example):

<SELECT MULTIPLE NAME='city'>
<OPTION VALUE=1>Rio d,Janeiro</OPTION>
<OPTION VALUE=2>Brasilia</OPTION>
<OPTION VALUE=3>Paris</OPTION>
<OPTION VALUE=4>Bonn</OPTION>
</SELECT>

Now if a user selects two of the options, e.g. Brasilia and Bonn, then the value coming back into your PHP script should read as "2,4"



If you haven't reached the end of the road and you're not at the start then you must be somewhere in the middle

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

IP logged posted posted 08-06-2003 17:50 Edit Quote

Or...

code:
(name the select city[] )
<SELECT MULTIPLE NAME='city[]'>
<OPTION VALUE=1>Rio d,Janeiro</OPTION> (selected)
<OPTION VALUE=2>Brasilia</OPTION>
<OPTION VALUE=3>Paris</OPTION> (selected)
<OPTION VALUE=4>Bonn</OPTION>
</SELECT>


This can be accessed in PHP in the array that now exists by the name: "$_POST['city']" (or $_GET['city'] whichever method you use in your form)
Like this:
$_POST['city'][0] = "1";
$_POST['city'][1] = "3";


/Dan




{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

IP logged posted posted 08-07-2003 02:56 Edit Quote

And, the XHTML way to write that would be

<select multiple="multiple" name="city[]">
<option value="1">rio d,janeiro</option>
<option value="2">brasilia</option>
<option value="3">paris</option>
<option value="4">bonn</option>
</select>

(just trying to be a pain in the ass)

DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

IP logged posted posted 08-07-2003 09:16 Edit Quote

Yeah, I know (cut'n paste)
Annoying is good as long as it's correct...
:P
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu