?
Solved

How can I get multiple values of List Box ?

Posted on 2003-03-13
34
Medium Priority
?
241 Views
Last Modified: 2007-12-19
I want to get multiple selected items in a list box when the form is sent using Javascript.
0
Comment
Question by:Pinak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 13
  • 3
  • +3
34 Comments
 
LVL 14

Accepted Solution

by:
avner earned 80 total points
ID: 8126980
<html>
<head>
<title>about:blank</title>
<script language="javascript1.2">
<!-- copyright(c) avcoh@yahoo.com
function getAllSelected(oSelect)
{
var arrO = new Array();
for (var i=0;i<oSelect.options.length;i++)
     {
     var oOption = oSelect.options[i];
          if (oOption.selected)arrO.push(oOption.value);
     }
     return arrO.join("~");
}
-->
</script>
<style>

</style>
</head>
<body>
<select id="aa" size="5" multiple="true">
     <option value="test1">test1</option>
     <option value="test2">test2</option>
     <option value="test3">test3</option>
     <option value="test4">test4</option>
     <option value="test5">test5</option>
     <option value="test6">test6</option>
</select>
<input type="button" onclick ="alert(getAllSelected(document.getElementById('aa')))" value="show Selected">
</body>
</html>
0
 
LVL 2

Expert Comment

by:kahwoo
ID: 8127381
Function getAllSelected() {
  var selectedList='';
  for( var cnt=0; cnt < form1.aa.options.length; cnt++) {
    if(form1.aa.options[cnt].selected) {
       if(selectedList!='')
          selectedList += '~';
       selectedList += form1.aa.options[cnt].value;
     }
  }
  return selectedList;
}

using that function it will return a string of items which is selected with the delimiter, just submit this value to the server side to process

0
 
LVL 2

Expert Comment

by:kahwoo
ID: 8127394
or if u let me know wat is ur server script may be i can help u on the server script as well.

U can use the server script to read all the selected values.. These values are in array form.

regards
kahwoo
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:avner
ID: 8127440
kahwoo , Your submitted method is exactly what I submitted There is no need of sending comments with the exact same information.
0
 

Expert Comment

by:EEK
ID: 8129794
Don't do it too difficult way. Well I don't know how it works with another server side scripts, but with ASP:

myvar = Request.Form("mylistbox")

If you have selected multiple values from the listbox then myvar has: 1,2,3,4... and you easilly get those with Split:

myvartable = Split(myvar, ",")

Now:

myvartable(0) = 1
myvartable(1) = 2
myvartable(2) = 3

etc.

I don't know how you handle the values that are sent via your from, but I think the principle is same with other Server Side Scripts too (PHP,JSP,ColdFusion). Meaning you don't have to split the values in client side, you can do it more easilly in server side.

-EEK
0
 
LVL 4

Expert Comment

by:whammy
ID: 8133081
I'll definitely side with EEK on this one!

Not to mention, if you're using ASP and a multiple select, using the IN() operator in SQL becomes a very efficient method of doing searches or whatnot.

I think we need more information... such as:

What exactly (not in code, but in approximate English, preferably) are you trying to accomplish, preferably with an example! - what server-side language are you using to validate the input - etc.?

I use select multiple in some database searches using ASP and it's "da bomb". They also run very fast.
0
 
LVL 2

Expert Comment

by:kahwoo
ID: 8135216
avner,

that is why in my second comment i have ask you to let us know wat server script tat you are using ..

EEK samples needs some touch to it.. u need to add some checking to it. His sample might be an easy way but u need to use Ubound() function to validate the ARRAY and u might want to use another loop to retreive all the value in the array

may be u can try this way

Dim ctr
Dim tempValue

for ctr=1 to Request("aa").count

   tempValue = Request("aa")(ctr)
   Response.write tempValue

next

However the way he did is very convenience and good in database query


myvar = Request.Form("mylistbox")

If Len(myvar) > 0 Then
   SQL= "SELECT * FROM Reservation WHERE year in (" & myvar & ")"
End If

whammy,

   sorry for being rude, but i dun care who side are u on

Clarification : I answer that way bcoz, i wanted to construct a url string for XML load method in my first comment. i am too used to XML
0
 

Author Comment

by:Pinak
ID: 8135274
I want to get values selected in the listbox in the variable I used for the listbox.
eg
<select name=optvalue multiple size="3">
<option value=abc>abc</option>
<option value=abc1>abc1</option>
<option value=abc2>abc2</option>
</select>

Now when I submit the form I want the values selected in the variable optvalue. For that I think I have to take it as a array variable. But how.....
0
 
LVL 14

Expert Comment

by:avner
ID: 8145806
Pinak , If you need the data for processing on the client side, then I suggested the method to get all the selected options.
If you need for the server side, then you don't need to change/add anything to the code, just give a certain name to the SELECT box :
<SELECT name="optvalue".....


And then read it's values using the server side scripting, as pepole suggested above use :
myvar = Request.Form("optvalue")

When dealing with ASP .

If this does not answer your question, you'll have to better clarify your problem.



0
 

Author Comment

by:Pinak
ID: 8150960
Avner, If I just name the listbox to "optvalue" without any coding then I dont get any multiple values which are selected in the variable, I just get one value which is selected first(as it is not an array variable I think).

When I submit my form, the action file which I had mentioned form the form is in PHP and linked with MySql database. I had just given the "optvalue" variable name to insert the values in the table.

Now, I want the multiple values which are selected to be the values of "optvalue" only not any second variable as u have suggested in ur method initially(using javascript only).
0
 
LVL 14

Expert Comment

by:avner
ID: 8150975
Pinak, this example demonstrate that when you select more then one element, it is sent to the server with more then one value, check you server code as this is your problem, after you click "Submit Queury", check the address bar, you will see that all selected values are beign sent :

<body>
<form>
<select id="aa" size="5" multiple="true" name="optvalue">
    <option value="test1" selected>test1</option>
    <option value="test2">test2</option>
    <option value="test3" selected>test3</option>
    <option value="test4">test4</option>
    <option value="test5">test5</option>
    <option value="test6">test6</option>
</select>
<input type="button" onclick ="alert(getAllSelected(document.getElementById('aa')))" value="show Selected">
<input type="submit">
</body>

0
 

Author Comment

by:Pinak
ID: 8151664
Avner, I m getting multiple values only on the client side through variable "arrO", but I want to carry on to the action file of my form through "optvalue".
0
 
LVL 14

Expert Comment

by:avner
ID: 8151708
Pinak , read through the thread again, this question is answer number of times by other people. your problem is just reading the data on the server side.
0
 

Author Comment

by:Pinak
ID: 8157924
How can I read data on the server side?
0
 
LVL 14

Expert Comment

by:avner
ID: 8157948
What server side language are you using ?

If you are using ASP (vbscript) use :
sOptionValue= Request.Form("optvalue").split(",")
0
 

Author Comment

by:Pinak
ID: 8157991
I am using Javascript.
0
 
LVL 14

Expert Comment

by:avner
ID: 8158010
Exactly the same in this case.



0
 

Author Comment

by:Pinak
ID: 8158016
What will be the exact syntax?
0
 

Author Comment

by:Pinak
ID: 8158022
What will be the exact syntax?
0
 
LVL 14

Expert Comment

by:avner
ID: 8158031
(remember that this is server side) :

var sOptionValue= Request.Form("optvalue").split(",")
0
 

Author Comment

by:Pinak
ID: 8158169
Avner, see my form is like this....
<FORM METHOD="post" ACTION="form_done.php" name="form">
<select name=optvalue multiple size="3">
<option value=abc>abc</option>
<option value=abc1>abc1</option>
<option value=abc2>abc2</option>
</select>
</FORM>

Now, in my form_done.php file I m inserting values into the Mysql database table like this....
$sql="INSERT INTO table_name (optionvalue) VALUES ('$optvalue')";
mysql_query($sql);

Now as I m using variable "optvalue" to insert the data into the table, so I want the multiple selected items in the listbox to be the values of "optvalue" only not other variables like "sOptionValue" and using javascript only not asp.
0
 
LVL 14

Expert Comment

by:avner
ID: 8158181
So you are not using Javascript , you are using PHP !

For PHP use :


    $sOptValue=$HTTP_POST_VARS["optvalue "]
0
 
LVL 14

Expert Comment

by:avner
ID: 8158187
What you could also do for PHP is change optvalue to optvalue [] :
<select name="optvalue[]" multiple size="3">
....

Then on the Server side :
$opt_values= $HTTP_GET_VARS['optvalue'];


and then $opt_values is an array of all selected values;


0
 

Author Comment

by:Pinak
ID: 8158194
Avner, what is use in shifting the value of optvalue to sOptValue as optvalue will be having only one value not multiple values.
0
 
LVL 14

Expert Comment

by:avner
ID: 8158204
Pinak , I do not understand your question, what part of the answer are you reffering too ?
0
 

Author Comment

by:Pinak
ID: 8158214
Avner, what is use in shifting the value of optvalue to sOptValue as optvalue will be having only one value not multiple values.
0
 

Author Comment

by:Pinak
ID: 8158223
Sorry it was for previous one
0
 
LVL 14

Expert Comment

by:avner
ID: 8158230
Let me know if you have any more questions.
0
 

Author Comment

by:Pinak
ID: 8158291
Avner, it is not working. u r referring server side as my form_done.php file...right.But its not picking up the values selected and the record entered is blank in the table.
0
 
LVL 14

Expert Comment

by:avner
ID: 8158296
Can you please post the code you came up with ?
0
 

Author Comment

by:Pinak
ID: 8158428
This is my form_post.php file

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<FORM METHOD="post" ACTION="form_done.php" name="form">
<select name=optvalue[] multiple size="3">
<option value=abc>abc</option>
<option value=abc1>abc1</option>
<option value=abc2>abc2</option>
</select>
<input type="submit" name="Submit2" value="Save">
</FORM>
</HTML>

This is my form_done.php file

<HTML>
<HEAD>
<TITLE></TITLE>
<?php
$db=mysql_connect("localhost",'') or die("Couldn't connect");
mysql_select_db("database_name",$db) or die("Couldn't select the database");
$opt_val=$HTTP_GET_VARS['optvalue'];

$sql="INSERT INTO table_name (optionvalue) VALUES ('$opt_val')";
mysql_query($sql);
?>
</HEAD>
<BODY>
The value is entered
</BODY>
</HTML>
0
 
LVL 14

Expert Comment

by:avner
ID: 8158465
Your code does not make much sense you made a great  mismatch.


$opt_val is an Array, you are using it as a string :(VALUES ('$opt_val')")

I'm not sure about what is the fix as I am not a PHP developer.
Yuo might want to ask for the fix on the PHP section.



0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10414134
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: avner {http:#8126980}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question