Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How can I set popup_menu parameters and send them to another page?

by jaffinito34 (Acolyte)
on May 11, 2013 at 18:00 UTC ( #1033141=perlquestion: print w/ replies, xml ) Need Help??
jaffinito34 has asked for the wisdom of the Perl Monks concerning the following question:

I understand to send data to another page, you use:

print start_form( -action=>'results.cgi'); various form stuff.... print end_form;

I would like to be able to take the selected options from popup_menu's as well as a textbox to return information from a database. So I'm going from a search page to a results page. The search page is displaying information from the database, then whatever the user picks as their selection in the popup_menu gets passed to the results page to query the database.


use DBI; use CGI::Cookie; use CGI qw /:standard/; use warnings; #checking if cookie is set, if not redirecting to login my %cookies = CGI::Cookie->fetch; if (! defined $cookies{'authorized'}) { print redirect('login.cgi') } #connecting to DB my $dbh = DBI->connect("dbi:SQLite:dbname=/path/to/database.db","","") +; # getting artist info my $sth = $dbh->prepare('select artistid, name from artist'); $sth->execute; # getting genre info my $sth2 = $dbh->prepare('select genreid, name from genre'); $sth2->execute; #printing header print header, start_html('Database Search'), h2('Select items from the dropdown menus to help filter your results.' +), br,br, end_html; print start_form(-action=>'results.cgi'); end_form; ### THIS CAUSES REDIRECT TO RESULTS WHEN USER HITS SUBMI +T ############################POPUPS###################### #displaying artist dropdown my @artistValues; my %artistLabels; #this push adds to beginning of values, then it gets set to 'Make a se +lection' as the default for the dropdown push (@artistValues, -1); print h4('Please select an artist'); $artistLabels{-1} = "Make a selection"; while (my @row = $sth->fetchrow_array) { #2 columns in DB, first is ID second is NAME my $artistId = $row[0]; my $artistName = $row[1]; push(@artistValues,$artistId); $artistLabels{$artistId} = $artistName; } print popup_menu(-name=>'artist', -values=>\@artistValues, -labels=>\%artistLabels); $userArtist = param('artist'); #displaying genre dropdown my @genreValues; my %genreLabels; push (@genreValues, 'Make a selection'); print br,br; print h4('Please select a genre'); $genreLabels{-1} = "Make a selection"; while (my @row = $sth2->fetchrow_array) { my $genreId = $row[0]; my $genreName = $row[1]; push (@genreValues, $genreName); $genreLabels{$genreId} = $genreId; } print popup_menu(-name=>'genre', -values=>\@genreValues, -lables=>\%genreLabels); $userGenre = param('genre'); print br,br; ###################################################################### +## #displaying trackname textbox print "Track name: ",textfield('trackname'),br,br submit('SEARCH'), $trackName = param('trackname'); while(my @row = $sth->fetchrow_array){ foreach $item (@row){ print "$item "; }print br; } print br,br,br,br,a({href=>'http://www.blablabla.com/logout.cgi'},'Log +out');

When I go to print the parameters ($userArtist, $userGenre, $trackName), only trackname gets printed and I don't see anything for artist or genre. I am not able to find anything online about this, or maybe I'm just unsure how it works, any help is appreciated.

Comment on How can I set popup_menu parameters and send them to another page?
Select or Download Code
Replies are listed 'Best First'.
Re: How can I set popup_menu parameters and send them to another page?
by poj (Priest) on May 11, 2013 at 19:15 UTC
    You could use a test results.cgi page to see the name and value of all the parameter sent like this ;
    use strict; use CGI qw /:standard/; print header,start_html('results'); print h3("Parameters"); for (param){ print "$_ = ".param($_)."<br/>"; } print end_html;
    poj
Re: How can I set popup_menu parameters and send them to another page?
by thewebsi (Scribe) on May 11, 2013 at 20:07 UTC

    I'm assuming that this script is the "search" page and the "results" page is where you are printing the parameter values (not shown here).

    print start_form(-action=>'results.cgi');
    end_form; ### THIS CAUSES REDIRECT TO RESULTS WHEN USER HITS SUBMIT

    It looks like this might be the issue: You want to print end_form() (not just run it), and you want to do this at the end of your script (ie, at the end of the form), not here.

Re: How can I set popup_menu parameters and send them to another page?
by Anonymous Monk on May 11, 2013 at 20:22 UTC
    #printing header print header, start_html('Database Search'), h2('Select items from the dropdown menus to help filter your results.' +), br,br, end_html;
    Why did you output the end_html here? That should be the last item printed, not one of the first items prior to your form.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1033141]
Approved by ww
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2015-07-29 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls