|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
referencing a multiple select box with an array name
Hi,
I've been trying to get a piece of Javascript and it's doing my head in. The code moves elements from one multiple select box to another (you'll see what I mean) This all works fine at the moment for the Javascript side of things,but I need to be able to read all of the values being submitted for my PHP query. To do that, I need to have a multiple select box called chosen[] instead of chosen from what I can tell but I'm not too handy with Javascript. I've tried simply replacing chosen with chosen[] in all instances, but not to much success, I get errors. Please could someone give me some advice on how to do this as I've been at it for several hours and can't do it. Thanks Code:
<script LANGUAGE="JavaScript">
<!--
function copyToList(from,to){
fromList = eval('document.forms[0].' + from);
toList = eval('document.forms[0].' + to);
if(toList.options.length >0&& toList.options[0].value == 'temp'){
toList.options.length =0;
}var sel =false;
for(i=0;i<fromList.options.length;i++){var current = fromList.options[i];
if(current.selected){
sel =true;
if(current.value == 'temp'){
alert ('You cannot move this text!');
return;
}
txt = current.text;
val = current.value;
toList.options[toList.length]=new Option(txt,val);
fromList.options[i]=null;
i--;
}}if(!sel) alert ('error');
}function allSelect() {
List = document.forms[0].chosen.selectedIndex;
if(List.length && List.options[0].value == 'temp')return;
for(i=0;i<List.length;i++) {
List.options[i].selected =true;
}}
// -->
</SCRIPT>
<form onSubmit="allSelect(); return" method="post">
<p>From:<br>
<select NAME="possible" SIZE="8" MULTIPLE>
<option value="1">One</option>
<option value="2">Two</option>
</select>
<aHREF="javascript:copyToList('possible','chosen')">Add to Booking</a><br>
<aHREF="javascript:copyToList('chosen','possible')">Remove from Booking</a>
<p>To:<br>
<select NAME="chosen" SIZE="8" MULTIPLE>
<option VALUE="temp">Make your choice on the left
</select>
<input TYPE=submit>
</form>
|
|
#2
|
||||
|
||||
|
So if i understand correctly, you have 2 list boxes. One is used to pass options across to the other (which is empty to start with?)
If so, you just need to modify your allSelect() function so that it adds each option found in the choosen select object, to a CSV'd string (strList.value += option value). Once that's done, place the value of the string in a hidden form field, that gets submitted with the form. |
|
#3
|
|||
|
|||
|
Maybe you're not getting the values you want because the values in a "select" list only get sent when they are selected. So unless you try the approach suggested above, you would need to use another javascript function which would select all elements in the "chosen" list before the submit. This would give you a string in the same form: 1, 2, etc., that you would need to split before using when receiving in PHP.
|
|
#4
|
|||
|
|||
|
Just tried Stumpy's idea and works spot on.
The allSelect() function selects all of the elements in the listbox anyway. Thanks guys, Andrew |
|
#5
|
||||
|
||||
|
My solution will also work if you do not have the list box set to "multiple".
|
|
#6
|
|||
|
|||
|
Andrew,
Would it be possible for you to post the solutions code here, as I'm also doing this and I am having no luck. Thanks Paul |
|
#7
|
|||
|
|||
|
U also used your javascript, thanks!
It works if you replace one of your lines in allSelect() by this one List = document.forms[0].chose; Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > referencing a multiple select box with an array name |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|