Reputation: 1567
I have a HTML form with two select elements(listboxes) and two buttons to move the data from one listbox to another(using JavaScript):
<form action="page.php" method="post">
<select name="select1" multiple="yes">
<option value="1">Left1</option>
<option value="2">Left2</option>
<option value="3">Left3</option>
</select>
<input type="button" value="-->" onclick="moveOptions(this.form.select1, this.form.select2);" /><br />
<input type="button" value="<--" onclick="moveOptions(this.form.select2, this.form.select1);" />
<select name="select2" multiple="yes">
<option value="4">Right1</option>
<option value="5">Right2</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
When I hit the Submit button I want to get all the values stored in the listboxes.
foreach ($_POST['select1'] as $value)
{
//save data to database
}
This just gets one selected value(if there was one).
I managed to get multiple values (if they are selected) by putting a []
after the name of the select
.
<select name="select1[]" multiple="yes">
But this still doesn't gets the unselected values, and this way the data moving JavaScript function doesn't works either.
Upvotes: 0
Views: 7888
Reputation: 318
Before the form is submitted just select all the options:
document.getElementsByName('submit')[0].onclick = function () {
var s1 = document.getElementsByName('select1')[0];
for(var i=0; i < s1.options.length; i++){
s1.options[i].selected = true;
}
};
Upvotes: 4
Reputation: 1282
var select1 = document.getElementById('select1');
var values = new Array();
for(var i=0; i < select1.options.length; i++){
values.push(select1.options[i].value);
}
To pass the array from page to page, go here
Upvotes: 1
Reputation: 601
I am not sure why you want to do this but you can call this Jquery code before submitting the form and add the array value to a hidden input to get to be posted
$("#Submit_button").click ( function() {
var arr = new Array;
$("#select1 option").each ( function() {
arr.push ( $(this).val() );
});
$(#hidden_input).val(arr);
});
Upvotes: 1