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!

Take a look at what I am working on 2

Status
Not open for further replies.

goldensunrise

Programmer
Nov 22, 2000
18
Take a look at what I am working on:
HERE'S THE QUESTION:

How do I change what shows up in the second box based on what is chosen in the first box? I've tried some javascript stuff but, have had no luck. Any help would be apreciated.

Here's the Code:

$main="images";
chomp $main;

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

opendir (MAIN, $main);
@dir =readdir MAIN;

#GENERATES CODE FOR MAIN CATEGORY DROP DOWN LIST BOX
print ('<SELECT name=&quot;mainCat&quot;>');
for ($i = 0; $i <= $#dir; $i++)
{
if ( $dir[$i] !~ /^\./ )
{
print ('<CENTER><OPTION');
if ( param(&quot;mainCat&quot;) && param(&quot;mainCat&quot;) eq $dir[$i])
{
print (' selected');

}
print ('>', $dir[$i], '</OPTION></CENTER>', br);
opendir (SUB, &quot;$main/$dir[$i]&quot;);
@sub=readdir SUB;
}
}
print ('</SELECT>');
print (&quot; &quot;);

#GENERATES CODE FOR SUB CATEGORY DROP DOWN LIST BOX
print ('<SELECT name=&quot;subCat&quot;>');
for ($a = 0; $a <= $#sub; $a++)
{
if ( $sub[$a] !~ /^\./)
{
print ('<CENTER><OPTION>', $sub[$a], '</OPTION></CENTER>', br);
}
}
print ('</SELECT>');
print (&quot; &quot;);

print (input({-type=>'submit', -value=>'View Pictures'}), br);
print end_form;
 
If what's in the box is dependent on what the client does, then I don't believe perl can do it. Perl (or CGI for that matter) is server-side, so things the client does (like check a box) can't dynamically change something in the page without reloading it and talking with the server. Javascript is a much better solution, so I'd encourage you to field the question over yonder (unless one of the perl web gods knows this one offhand?) ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
I had tried using javascript but, couldn't get it to work right since I was dynamically populating the list box with elements from an array.
 
I don't understand the code:

if ( $dir[$i] !~ /^\./ )
{
print ('<CENTER><OPTION');
if ( param(&quot;mainCat&quot;) && param(&quot;mainCat&quot;) eq $dir[$i])
{
print (' selected');


Why are you checking param(&quot;mainCat&quot;) twice? &quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
You can do this but you need to dynamically populate the javascript code also to reflect the choices based on the first selection. Take your dynamic insertion of code one step further.

look into the eval{} function in the man pages. It is a good place to build code and execute it dynamically.
 
#CHECKS TO SEE IF $DIR[$I] STARTS WITH A DOT IF IT DOES, IT WONT PRINT.

if ( $dir[$i] !~ /^\./ )
{
print ('<CENTER><OPTION');
#WHEN YOU CLICK THE SUBMIT BUTTON, THIS CODE MAKES THE OPTION YOU CHOSE BE SELECTED.
if ( param(&quot;mainCat&quot;) && param(&quot;mainCat&quot;) eq $dir[$i])
{
print (' selected');
}
}
 
So i really hacked your code up but i think it does what you are trying to do. I used the popup_menu() from CGI.pm (since you are already using the module anyway)
Code:
my $main=&quot;images&quot;;
# No need to chomp $main

opendir (MAIN, $main) or die $!;
my @dir = grep { -d &quot;$main/$_&quot; and ! /^\./ } readdir MAIN;
my (@sub,$dir);

print start_form({  -name => 'form1',
                    -enctype=>'multipart/form-data',
                    -method=>'POST',
                }),
        popup_menu( -name   => 'mainCat',
                    -values => [ '--', @dir ],
                    -default=> '--',
                    -onChange => 'javascript:form1.submit()',
                ),
        end_form, &quot; &quot;;

print start_form({  -name => 'form2',
                    -enctype=>'multipart/form-data',
                    -method=>'POST',
                    -action=>'category_listing2.cgi',
                });
if ($dir = param('mainCat')) {
    opendir (SUB, &quot;$main/$dir&quot;);
    @sub = map { &quot;$main/$dir/$_&quot; } grep { -d &quot;$main/$dir&quot; and ! /^\./ } readdir SUB;
}
print   popup_menu( -name   => 'subCat',
                    -values => [ @sub ],
                    -labels => { map { $_, m!/([^/]*)$! } @sub },
                );
print   &quot; &quot;;

print input({-type=>'submit', -value=>'View Pictures'}), br();

print end_form;
You might have to play with it to get it to work for you but it will give you a place to start. Clicking the 'submit' button will pass the full relative path of the image file in the subCat param.

jaa
 
Oops, in this line
Code:
@sub = map { &quot;$main/$dir/$_&quot; } grep { -d &quot;$main/$dir&quot; and ! /^\./ } readdir SUB;
The '-d &quot;$main/$dir&quot;' is unnecessary. It should just be
Code:
@sub = map { &quot;$main/$dir/$_&quot; } grep { ! /^\./ } readdir SUB;
jaa
 
Ok, now when I look back at the code that all should have been obvious the first time. Thanks!
&quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
Justice,

Thank you soooooooo much. That code worked perfectly.

Thank You
 
Justice,

I tried changing the form action to the following:
filelisting.cgi?mainCat=$dir&subCat=$_

I can get the what's in $dir to show but, not what's in $_.

Thank you
 
[tt]$_[/tt] isn't set at this point in the code. It only exists inside the map block in this instance. I don't see why you are url encoding the query variables when you are using the POST method. The parameters from form2 get passed in the QUERY_STRING environmental variable in a POST request, not in the url. If you want to have the 'mainCat' parameter passed to your post then put it as a hidden field under form2. Put this somewhere between the form2 begin and end:
Code:
print hidden( -name   => 'mainCat',
              -default=> $dir,
            );
then in your category_listing2.cgi you will be able to acces these parameters with [tt]param('mainCat')[/tt] and [tt]param('subCat')[/tt]. I think that the CGI ignores url encoded parameters when a POST request is make.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top