Home: Perl Programming Help: Intermediate:
Drop DownMenu-option selected



cathy
newbie

Sep 29, 2000, 8:38 AM


Views: 1546
Drop DownMenu-option selected

On a web page, I display a drop-dwon menu where the user can select one name from a list of names. I save the selected name to a file. When the user goes back to view the list (there are other fields, of course), I would like to again display ALL the names in the list but have the name they previously selected show up as selected with the other names available for selection (the list of names are in a flat file). This can be done using ASP, so it seems it should be possible in cgi. In ASP, the code is something like: <option> </option><option <% If rs("QuarterRelease")= "" Then %>selected<% End if %>></option>
I tried adapting this into cgi, but it didn't seem to work. Any suggestions/examples?


dws
Deleted

Sep 28, 2000, 10:58 PM


Views: 1546
Re: Drop DownMenu-option selected

"It didn't seem to work" covers a lot of ground. Show us your code, and tell us what you observed.


cathy
newbie

Sep 29, 2000, 11:20 AM


Views: 1546
Re: Drop DownMenu-option selected

"it didn't work" means it didn't perform as I had hoped it would--display the full list but have the previously selected item highlighted.

Unfortunately I deleted the code that did not perform as desired, but I can try to reconstruct it.


cathy
newbie

Sep 29, 2000, 11:38 AM


Views: 1546
Re: Drop DownMenu-option selected

Here's the code:
print "<form method=$POST action=$action>";
open(RESOURCES, "</usr/local/httpd/cgi-bin/admin/resources.txt")&#0124; &#0124;&ErrorMessage;
@people=<RESOURCES>;
close(RESOURCES);
$totalresources = @people;
$resourceindex=0;
$size="10";
$name="Resource";
print "<tr><td><select size=$size name=$name>";
print "<option> </option>";
while ($totalresources > 0){
print "<option if ($people[$resourceindex] =~m/$lines[$record+3]/){selected}</op
tion>";
--$totalresources;
++$resourceindex;
}

When I execute the file, the drop-down menu displays but there are no items in the menu.


dws
Deleted

Sep 29, 2000, 2:12 PM


Views: 1546
Re: Drop DownMenu-option selected

I suggest laying out the HTML first, then working backwards to get the Perl you need.

You'll end up with something like this
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$selected = $lines[$record + 3]; # i assume this is the person previously selected
...
print "<select name=$name size=$size>\n";
print "<option> </option>\n";
foreach my $person ( @persons ) {
print "<option value=\"$person\"";
print " selected" if $person eq $selected;
print ">$person</option>\n";
}
print "</select>\n";</pre><HR></BLOCKQUOTE>


[This message has been edited by dws (edited 09-29-2000).]


cathy
newbie

Sep 29, 2000, 4:02 PM


Views: 1546
Re: Drop DownMenu-option selected

It works! Thanks.