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

Hi Monks
I am creating a popup_menu each time I am fetching a new set of values from the database. But I am running into a problem as default values of popup_menus are retained across queries.
I am using a conditional statement as below, please help me with a way so that every time it is created the previous values are not maintained.
I think I am making some syntax error and have to mention it as an array ref but not sure how can I do that while using a conditional statement

while(defined (my $row = $stmt->fetchrow_hashref)) { my $x = $row->{CONNECTION}; my $y = $row->{CARRY}; my $drop_down = $cgi->popup_menu( -name => "CHOICE", -values => [ 0 .. 6 ], -labels => { 0 => 'NO', 1 => 'A', 2 => 'B', 3=> 'C', 4 => 'D', 5 => 'E', 6 => 'F' }, -default => ($row->{CONNECTION} eq 'Y' && $row->{CARRY}) ? $row->{CARRY}: 0, );

When the above mechanism did not work, I tried the following one as well i.e taking the decision first in an if-else and then assigning the value but it did not work either :

while(defined (my $row = $stmt->fetchrow_hashref)) { my $x = $row->{CONNECTION}; my $y = $row->{CARRY}; my $default; if ($x eq 'Y') { if ($y) { $default = $y; } } my $drop_down = $cgi->popup_menu( -name => "CHOICE", -values => [ 0 .. 6 ], -labels => { 0 => 'NO', 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E', 6 => 'F' }, -default => $default, );

Replies are listed 'Best First'.
Re: popup_menus retaining values across queries
by Anonymous Monk on Sep 01, 2014 at 12:21 UTC
    Keeping the last submitted value is a feature of CGI. Try $cgi->delete("CHOICE"); before creating the popup menu.
      thanks, it has worked.
Re: popup_menus retaining values across queries ( CGI->new({}) -override => 1 )
by Anonymous Monk on Sep 01, 2014 at 21:18 UTC

    Like the other AM said, its a feature , but while delete will work, so will -override and creating a new cgi object

    #!/usr/bin/perl -- use CGI; use strict; use warnings; my $q = CGI->new({1,-sham}); print join qq{\n}, $q->textfield(-name,1,-value,2), $q->textfield(-name,1,-value,2, -override,1), CGI->new({})->textfield(-name,1,-value,2), ;;;;;;; __END__ <input type="text" name="1" value="-sham" /> <input type="text" name="1" value="2" /> <input type="text" name="1" value="2" />

    If you don't want any of the previous values, create new a object initialized from a hash  CGI->new({})

    If you want to override some, don't delete them, -override => 1

      use CGI qw( -nosticky ); also disables sticky values, as documented in "Pragmas" in the CGI documentation.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)