Moving data between select fields
Posted: Mon May 26, 2008 2:32 am
Ok, I've got a form that allows users to enter data into a database. In that form there is a select field populated with counties like so:
Later I'm filling $display_block with html code. There is one select box that holds the counties and another that is blank. Two buttons (add, remove) are in-between the two tables. users select which county they want to add to the record and click the add button - it moves over to the blank select field. This is the code for those fields:
Finally, the javascript i use to control those buttons is as follows:
The addrecord.php script that processes this form always seems to think the value in the select box with name="geo_scope[]" and id="2" is NULL. Does anyone have any idea why this is happening? I want the options that move over to the second select box to have the same text (they do) and the same VALUE as the select box from which they originated (but the VALUE doesn't seem to stay the same, or else the variable is not being passed correctly to the addrecord script). Anyone have any ideas?
sage
Code: Select all
// initialize variables
$states = array('Statewide','Adams County','Asotin County','Benton County','Chelan County','Clallam County','Clark County','Columbia County','Cowlitz County','Douglas County','Ferry County','Franklin County','Garfield County','Grant County','Grays Harbor County','Island County','Jefferson County','King County','Kitsap County','Kittitas County','Klickitat County','Lewis County','Lincoln County','Mason County','Okanogan County','Pacific County','Pend Oreille County','Pierce County','San Juan County','Skagit County','Skamania County','Snohomish County','Spokane County','Stevens County','Thurston County','Wahkiakum County','Walla Walla County','Whatcom County','Whitman County','Yakima County');
$option_block1 = "";
$display_block = "";
// populate states dropdown
for ($i=0; $i < 40; $i++) {
$option_block1 .= "<option value=\"$states[$i]\">$states[$i]</option>";
}Code: Select all
<td><select name=\"geo_scope0[]\" style=\"width:auto\" id=\"1\" size=\"6\">$option_block1</select><button type=\"button\" onClick=\"putIn(1,2)\">Add >></button><button type=\"button\" onClick=\"putIn(2,1)\"><< Remove</button><select name=\"geo_scope[]\" id=\"2\" style=\"width:auto\" size=\"6\"></td>Code: Select all
<script type="text/javascript">
function putIn(to,from){
var x = document.getElementById(to);
var i = document.getElementById(from);
var opt = new Option(x.options[x.selectedIndex].text, x.options[x.selectedIndex].value);
i.add(opt, undefined);
x.remove(x.selectedIndex);
}
</script>sage