Pinak
asked on
How can I get multiple values of List Box ?
I want to get multiple selected items in a list box when the form is sent using Javascript.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
U can use the server script to read all the selected values.. These values are in array form.
regards
kahwoo
kahwoo , Your submitted method is exactly what I submitted There is no need of sending comments with the exact same information.
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
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
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.
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.
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
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
ASKER
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.....
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.....
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.
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.
ASKER
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).
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).
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</optio n>
<option value="test3" selected>test3</option>
<option value="test4">test4</optio n>
<option value="test5">test5</optio n>
<option value="test6">test6</optio n>
</select>
<input type="button" onclick ="alert(getAllSelected(doc ument.getE lementById ('aa')))" value="show Selected">
<input type="submit">
</body>
<body>
<form>
<select id="aa" size="5" multiple="true" name="optvalue">
<option value="test1" selected>test1</option>
<option value="test2">test2</optio
<option value="test3" selected>test3</option>
<option value="test4">test4</optio
<option value="test5">test5</optio
<option value="test6">test6</optio
</select>
<input type="button" onclick ="alert(getAllSelected(doc
<input type="submit">
</body>
ASKER
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".
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.
ASKER
How can I read data on the server side?
What server side language are you using ?
If you are using ASP (vbscript) use :
sOptionValue= Request.Form("optvalue").s plit(",")
If you are using ASP (vbscript) use :
sOptionValue= Request.Form("optvalue").s
ASKER
I am using Javascript.
Exactly the same in this case.
ASKER
What will be the exact syntax?
ASKER
What will be the exact syntax?
(remember that this is server side) :
var sOptionValue= Request.Form("optvalue").s plit(",")
var sOptionValue= Request.Form("optvalue").s
ASKER
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.
<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.
So you are not using Javascript , you are using PHP !
For PHP use :
$sOptValue=$HTTP_POST_VARS ["optvalue "]
For PHP use :
$sOptValue=$HTTP_POST_VARS
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;
<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;
ASKER
Avner, what is use in shifting the value of optvalue to sOptValue as optvalue will be having only one value not multiple values.
Pinak , I do not understand your question, what part of the answer are you reffering too ?
ASKER
Avner, what is use in shifting the value of optvalue to sOptValue as optvalue will be having only one value not multiple values.
ASKER
Sorry it was for previous one
Let me know if you have any more questions.
ASKER
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.
Can you please post the code you came up with ?
ASKER
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("localho st",'') or die("Couldn't connect");
mysql_select_db("database_ name",$db) or die("Couldn't select the database");
$opt_val=$HTTP_GET_VARS['o ptvalue'];
$sql="INSERT INTO table_name (optionvalue) VALUES ('$opt_val')";
mysql_query($sql);
?>
</HEAD>
<BODY>
The value is entered
</BODY>
</HTML>
<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("localho
mysql_select_db("database_
$opt_val=$HTTP_GET_VARS['o
$sql="INSERT INTO table_name (optionvalue) VALUES ('$opt_val')";
mysql_query($sql);
?>
</HEAD>
<BODY>
The value is entered
</BODY>
</HTML>
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.
$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.
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
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
var selectedList='';
for( var cnt=0; cnt < form1.aa.options.length; cnt++) {
if(form1.aa.options[cnt].s
if(selectedList!='')
selectedList += '~';
selectedList += form1.aa.options[cnt].valu
}
}
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