views:

217

answers:

2

Hi,

I have a selectbox on a form - which I've turned in to a list box by putting

 <select id="Select1" name="D1" size="5" style="width: 220px">

I'm filling this select/listbox with values...

When I post the form how can I get all the values in the select box..is this possible or am I only able to get one that has been selected.

Trouble is I want all the values in the select (I'm not selecting any as such)

Any ideas?

+1  A: 

Before submitting the form you can use some JavaScript to pull the items out of the select and put them into a hidden text field (as a delimited string)

For example, you can get the values using

var select1 = document.getElementById('select1');
var values = new Array();

for(var i=0; i < select1.options.length; i++){
    values.push(select1.options[i].value);
}

var allValues = values.join(";");
alert(allValues);

Hope that helps.

Paul Lucas
ok..I think this is the way to go. but I'm trying to get each value in the select but am having problems getting the value. what i have is : function doSelects() { var select1=document.getElementById("select1"); for (s1 = 1; s1 <= select1.length; s1++) { alert(select1.options[s1].value); } }.........how do output each value?
thegunner
is it select1.options[s1].value this is obviously incorrect but don't know the correct syntax
thegunner
I've updated my answer with an example that works for me. You will want run your for loop starting at zero, and ending at one less than options.length since options is a zero-based array.
Paul Lucas
+1  A: 

How are you adding the values to the list box? Are they static or are they pulled from a database.

If you're pulling from the database I would create a function that you use to get the data and bind to the list box.

Then use that same function when you want to get those values after the post. You may have to use some hidden fields to pass along any parameters you use to get the values for the list box in the first place.

example:

function get_models_for_make(int make_id)
  mydata_rs = SELECT name, id FROM models WHERE make_id = make_id
  return mydata_rs
end

so you could use this data to bind the objects to your listbox and also use it to get the values later that you did bind to your list box.

Johnsonch