Forum Moderators: open

Message Too Old, No Replies

Trouble setting index of select boxes name with javascript

using a form name array

         

RenardMF

7:32 pm on Aug 23, 2006 (gmt 0)

10+ Year Member



Hey, I'm trying to populate a series of drop down boxes that contain text and numerical indicies as option values from a database. I'm using php to populate the select boxes with the different options and then javascript to set the selectedIndex to correspond with input that the user has previously submitted to the form.

The function I am using to find out what the selected index of the value in question is:

function getOptionIndex(theElement) { 

var select = theElement; //get a reference to the select object

for (var i = 0; i < select.options.length; i ++) {

if (select.options[i].value == yy)

return i;

}

return -1;

}

The different select box name parameters, which repeat themselves, are labeled with the name "activity[]." So to get each of these elements to show what was previously selected I use the format: document.form.elements[activity[]][i], inside a php loop. This is working fine for me expect when i try to access the element document.form.elements[activity[]][0], which doesn't do anything or give me any errors for that matter.

Is there anything I am doing wrong? or any suggestions to make this work better?

Thanks in advance,
Andy

rocknbil

8:14 pm on Aug 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Andy!

Well for one,

if (select.options(i).value == yy)
Removed [, was italicizing post

I don't see where yy is populated? But still, maybe you need to rethink this part. This is a good example of when other things available to you are better for the task than Javascript.

I'm using php to populate the select boxes with the different options ... (then).... set the selectedIndex to correspond with input that the user has previously submitted to the form.

If you do this in your php output you eliminate the dependence on Javascript to set this index. I will use a perl-y example, you can extrapolate it for php.

I store my form values in the associative array (hash) %qs. When the form loads, there's no value in $qs{'my_select'}, so it will by default select the first element, which should be BLANK. As soon as someone selects something and submits it, $qs{'my_select'} now has a value. Watch carefully and note how my fat fingers never leave the keyboard:

$selectList = &getSelect('my_select');


sub getSelect {
my ($list,$listname,$sth,$rv,$id,$title,$i);
## passed parameter, the list NAME, in this case 'my_select'
$listname = shift(@_);
$list = qq¦<select name="$listname" id="$listname">
<option value="">Select</option>
¦;
$sth=$dbh->prepare("select id,title from my_option_table;");
$sth->execute;
while (($id,$title) = $sth->fetchrow_array) {
$list .= qq¦<option value="$id"¦;
if ($qs{$listname} eq $id) { $list .= ' selected'; }
$list .= qq¦>$title</option>\n¦;
}
$sth->finish;
$list .= '</select>';
return $list;
}

SO! Before the form loads, make a call to a sub that builds the list, if there's form values submitted that's what will be selected. No need to complicate it for yourself or the user by adding Javascript.

RenardMF

3:12 pm on Aug 28, 2006 (gmt 0)

10+ Year Member



Oh you're right! I forgot about doing it that way. Thanks for your help I really appreciate it.