http://www.perlmonks.org?node_id=549788

dannoura has asked for the wisdom of the Perl Monks concerning the following question:

hi,

I have two CGI scripts which work with a database. The database has filenames as keys and descriptive tags (in an array) as values. The first script enables the user to search through the database for files, based on the tags. The list produced by the search has (for eac hit) a link to the second script with one parameter: the filename.

./add_remove_tags.pl?movie=some_movie

The second script produces a page which lists the tags for the file and enables the user (via a form) to add or remove tags. Each time the user presses the Add/Remove Tags button he'll end up on the same page, with the new tags (if any). The problem is that the script is not passing itself the movie parameter. It's my understanding that reinvoking the script should add the URL parameter to the others, but this doesn't work. I tried adding it via a hidden field, but that didn't work either. The code is below. Any suggestions?

#! c:/Perl/bin/Perl.exe -w use strict; use CGI; use CGI::Carp qw/fatalsToBrowser/; use DBM::Deep; use URI::Escape; my $q = CGI->new(); my %params = $q->Vars(); # Start page my $movie = uri_unescape($params{'movie'}); print $q->header( "text/html" ); print $q->start_html(-title => $movie); print $q->h1($movie); my $db = new DBM::Deep ("tag_test.db"); foreach my $param (keys %params) { if ($param eq 'add_tags') { # Add tags my @add_tags = split /,/, $params{$param}; push @{$db->{$movie}}, @add_tags; } elsif ($param ne 'Add/Remove Tags') { for (0..$#{$db->{$movie}}) { # Remove tags if ($param eq $db->{$movie}[$_]) { splice @{$db->{$movie}}, $_, 1; last; } } } } # Start form print $q->start_form(); print $q->textfield(-name => "add_tags"); print $q->table([map {$q->Tr( $q->checkbox(-name => $_))} @{$db->{$movie}}]); print $q->submit(-name => 'Add/Remove Tags'); print $q->end_form(); print $q->end_html;

Replies are listed 'Best First'.
Re: passing parameters to CGI script
by rinceWind (Monsignor) on May 16, 2006 at 15:16 UTC

    Pass it in as a hidden field in your form, see Creating a hidden field

    Update: sorry, you said a hidden field didn't work. This is the normal way of doing this sort of thing. Please post your attempt to use a hidden field.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: passing parameters to CGI script
by ptum (Priest) on May 16, 2006 at 15:35 UTC

    As the wizzard noted, passing a parameter through a hidden field is often the way this is accomplished. One mistake many people make is that they don't put the hidden field inside the proper form ... so it isn't passed when the user clicks on the form submit button. As suggested, please show us the code using hidden() that didn't work. :)


    No good deed goes unpunished. -- (attributed to) Oscar Wilde

      ++ptum!! You hit the nail on the head. I put the hidden field out side the form in my previous attempt. Works fine now.