Click to See Complete Forum and Search --> : Pass select menu


Drewser2001
07-09-2003, 10:13 AM
Right now, I have a javascript that dynamically creates a select menu which the user can edit and delete items from (javascript as well). I want to be able to store the entire contents of the select menu (selected items or not) as a perl variable of somesort in format that can be used to generate the entire selecte menu again for create/edit/delete purposes.

I am storing it in a html file, but the variables are stored just as text to be read with a cgi script to show the info again for modification.

I tried putting everything in a text box, but because of the nature of the data (many white spaces), it is nearly impossible to separate it out into the original select elements.

I'm sure exactly how to make this happen, any help would be greatly appreciated.


Here's some of the code:

CREATES THE SELECT MENU

<input type=button value="Add" onClick="move(this.form.fun,this.form.na,this.form.addapprovers,this.form.approvers)" name="C1">
<input TYPE=button value="Delete" onClick="remove(this.form.addapprovers,this.form.approvers)" name="C2">
<input TYPE=button value="Edit" onClick="edit(this.form.fun,this.form.na,this.form.addapprovers,this.form.approvers)" name="C3"><BR>
<select NAME="addapprovers" width=500 style="Width: 500px" size=5></select>
<input type=hidden name="approvers">

function move(fbox1,fbox2,tbox,obox){
var i = 0;
if(fbox1.value != ""){
var no = new Option();
no.value = fbox1.value + " " + fbox2.value;
no.text = fbox1.value + " " + fbox2.value;
tbox.options[tbox.options.length] = no;
fbox1.value = "";
fbox2.value = "";
}
putinhiddenbox(tbox,obox);
}

function putinhiddenbox(box,hbox,obox){
var hiddenbox = "";
for(var i=0; i<box.options.length; i++){
hiddenbox += box.options[i].value + "\\n";
}
hbox.value = hiddenbox;
}

function remove(box,obox){
for(var i=0; i<box.options.length; i++){
if(box.options[i].selected && box.options[i] != ""){
box.options[i].value = "";
box.options[i].text = "";
}
}
BumpUp(box);
putinhiddenbox(box,obox);
}

function edit(fbox1,fbox2,box,obox){
for(var i=0; i<box.options.length; i++){
if(box.options[i].selected && box.options[i] != ""){
var values = box.options[i].value.split(" ");
box.options[i].value = "";
box.options[i].text = "";
fbox1.value=values[0];
for(var j=1; j<values.length; ++j){
fbox2.value += values[j] + " ";
}
}
}
BumpUp(box);
putinhiddenbox(box,obox);
}

function BumpUp(abox) {
for(var i = 0; i < abox.options.length; i++) {
if(abox.options[i].value == "") {
for(var j = i; j < abox.options.length - 1; j++) {
abox.options[j].value = abox.options[j + 1].value;
abox.options[j].text = abox.options[j + 1].text;
}
var ln = i;
break;
}
}
if(ln < abox.options.length) {
abox.options.length -= 1;
BumpUp(abox);
}
}

Jeff Mott
07-10-2003, 11:01 PM
I'm not entirely sure I understand what it is you need to do. But can store all the label/values for the options of a select menu into an array (a hash would be easier but wouldn't maintain the options order).
use CGI;
my $cgi = CGI->new();
my @options = (['Label1', 'Value1'], ['Label2', 'Value2']);
print q`<select name="some name">`;
print q`<option value="`, $cgi->escapeHTML($$_[1]), q`">`,
$cgi->escapeHTML($$_[0]), q`</option>` for @options;
**Note: code untested.