Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically Populated Drop Down List Box

Status
Not open for further replies.

goldensunrise

Programmer
Nov 22, 2000
18
Can anyone help me out?? I can't seem to get this to work right. It always goes back to the first item in the list. I would be greatful for any help.

to see it (not) working, go here:
***********
THE CODE
***********

print start_form({-enctype=>'multipart/form-data', -method=>'POST', -action=>'category.cgi'});

open ( CATEGORY, "category.txt" ) or die ( "Error opening category.txt"); #Opens text file for read
open ( SUB, $catfile ) or die ( "Error opening $catfile"); #Opens text file for read
#GENERATES CODE FOR MAIN CATEGORY DROP DOWN LIST BOX
print ('<SELECT name=&quot;mainCat&quot;>');
while ( <CATEGORY> ) {
$category = $_;
chomp $category;
print ('<CENTER><OPTION>', $category, '</OPTION></CENTER>', br);
}
print ('</SELECT>');
print (br, br);
#GENERATES CODE FOR SUB CATEGORY DROP DOWN LIST BOX
print ('<SELECT name=&quot;subCat&quot;>');
while ( <SUB> ) {
$sub = $_;
chomp $sub;
print ('<CENTER><OPTION>', $sub, '</OPTION></CENTER>', br);
}
print ('</SELECT>');
print (br, br);
close( SUB ) or die( &quot;Cannot close $catfile&quot; ); #Close text file
close( CATEGORY ) or die( &quot;Cannot close category.txt&quot; ); #Close text file
print (input({-type=>'submit'}), br);
print end_form;
 
open ( SUB, $catfile ) or die ( &quot;Error opening $catfile&quot;); #Opens text file for read

open ( SUB, [red]&quot;[/red]$catfile[red]&quot;[/red] ) or die ( &quot;Error opening $catfile&quot;); #Opens text file for read

Are you sure this file is populated? Sometimes little typos like this will make the difference. Try it an see... next is $catfile define properly. Try adding a few print statments for debugging like
$debug = 1;
print &quot;SUB source location: $catfile&quot; if ($debug == 1);

 
You don't need to put quotes around [tt]$catfile[/tt].

Your problem is that you're not actually saying which option should be selected. Here's what you'd do for the main category to make it work:

[tt]while (<CATEGORY>) {
$category = $_;
chomp $category;
print '<CENTER><OPTION value=&quot;$category&quot;';
print ' selected' if ((param('mainCat') &&
(param('mainCat') eq $category));
print '>$category</OPTION></CENTER>', br;
}
[/tt]

This would result in the <OPTION> for the selected main category (say &quot;Books&quot;) looking like:
[tt]<OPTION value=&quot;Books&quot; selected>Books</OPTION>[/tt]
 
rosenk,

Thanks for the help.
With a little tweaking that bit of code works perfect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top