in reply to Detect and reset hashes

What's parsing your POST data? Neither CGI.pm nor CGI::Lite gives you a hash for a multiple select.

I recommend using an interface that always gives you back the same thing, if possible. For example, CGI::param in list context returns a list of the values associated with that param, whether there's zero, one, or several values. This avoids adding special cases to the code that's calling it.

Replies are listed 'Best First'.
Re^2: hashes
by Anonymous Monk on Jun 07, 2004 at 19:44 UTC
    " What's parsing your POST data? Neither CGI.pm nor CGI::Lite gives you a hash for a multiple select." Hmmmm.. I used CGI.pm to retrive the post variables. If it returns a list, it is a big problem for me because i need keys to keep options highlighted in the menu in case of any error, missing values... I posted my code below... Inside the check function I included php functions (i have no idea about their perl equivalents) named "is_array" and "empty" to indicate where i need to check the post variable... I am new to Perl, and I translated this code from a php class that i have created for my projects. So it might have a lot of flaws that i am not aware of... THanks a lot for the responses...
    # this is the sub that creates the select menu sub elementSelect { my($self,$nm)=@_; # unique keys are data/sql, default # I use data key to create static select menus # sql key to create dynamic menus from tables my $data_array; my $row; my $multiple="no"; my $select=undef; if($self->{ELEMENTS}->{$nm}->{data}){ $data_array=$self->{ELEMENTS}->{$nm}->{data}; }elsif($self->{ELEMENTS}->{$nm}->{sql}){ my $sql=$db->prepare($self->{ELEMENTS}->{$nm}->{sql}); $sql=$db->execute(); while($row=$sql->fetchrow_arrayref){ $data_array->[$row->[0]]=$row->[1]; } } $multiple="yes" if defined $self->{ELEMENTS}->{$nm}->{attributes}->{ +multiple}; $select="<select name='".$nm; if($multiple eq "yes"){ $select.="[]"; } $select.="' ".$self->getAttributes($nm).">"; if($multiple eq "no"){ $select.="<option value=''>select ".$self->{ELEMENTS}->{$nm}->{def +ault}."</option>"; } if($multiple eq "yes"){ while(my($key,$value)=each(%{$data_array})){ my $selected; if(($self->{PVARS}->{$nm} && $self->{PVARS}->{$nm}->{$key}) || ( +!$self->{PVARS}->{$nm} && $self->{VARS}->{$nm}->{$key})){ $selected="selected"; }else{ $selected=""; } $select.="<option value='".$key."' ".$selected.">".$value."</opt +ion>"; } }else{ while(my($key,$value)=each(%{$data_array})){ my $selected; if(($self->{PVARS}->{$nm} && $key eq $self->{PVARS}->{$nm}) || + (!$self->{PVARS}->{$nm} && $key eq $self->{VARS}->{$nm})){ $selected="selected"; }else{ $selected=""; } $select.="<option value='".$key."' ".$selected.">".$value."</optio +n>"; } } $select.="</select>"; return $select; } ##################################################### # DATA VALIDATION FUNCTIONS # this is where i have the problem, the checkRequired sub # should be compatible with both single values and lists in # case of multiple selects ###################################################### sub checkRequired { my($self,$nm)=@_; my $o=""; if(is_array($self->{POST}->param($nm))){ # check for multiple select if(empty($self->{POST}->param($nm))){ # check the array has any values in it # maybe if(self->{POST}->param($nm))??? push @{$self->{ERRORS}},"Please enter ".$self->{ELEMENTS}->{$nm} +->{message}."!"; }else{ $o=$self->{POST}->param($nm); } }else{ # value is scalar if($self->{POST}->param($nm) eq ""){ push @{$self->{ERRORS}},"Please enter ".$self->{ELEMENTS}->{$nm} +->{message}."!"; }else{ $o=$self->{POST}->param($nm); } } return $o; }