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

Hello, This may be the worng place to ask a question, but I think I am right. I have already searched some of the archives to look for an answer and I can't find anything. Anyway, if my post is in the worng place, please direct me to the correct location. PROBLEM: I am trying to get some default values to be set in a formbuilder generated form. We should be able to pass these values to the formbuilder object by way of a hash. Here is the code from the formbuilder website.
#!/usr/bin/perl use DBI; use CGI::FormBuilder; $user = $ENV{REMOTE_USER}; # from .htaccess $dbh = DBI->connect(...); # your db here $sth = $dbh->prepare("select * from pers where user = $user"); $defs = $sth->fetchrow_hashref; @fields = qw(first_name last_name email phone address city state zip mail_list); $form = CGI::FormBuilder->new( method => 'post', fields => \@fields, values => $defs, # values from hashref required => 'ALL' );
Here is the script that I am trying to make work.
#!/usr/bin/perl -w use strict; use Template; use CGI; use CGI::FormBuilder; use Connect; use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; my $cgi = new CGI; my $defs ; # hash for default values my $dbh = data_connect(); # link to module that craetes DBI connection #get the default values to load the form my $stmt = "SELECT * FROM imp_lodging WHERE visitor_id = ? AND check_out IS NULL"; my $sth = $dbh->prepare($stmt); $sth->execute($cgi->param('visitor_id')); $defs = $sth->fetchrow_hashref; # for debugging to se if the hash was populated for ( keys %{$defs} ){ print "$_ => $defs->{$_} <br>"; } my $form = CGI::FormBuilder->new( action => 'edit_visitors.pl', method => 'post', fields => [qw(visitor_id location check_in chec +k_out)], values => $defs, validate => { location => 'INT', check_in => '/^[0-1]\d-[0-3]\d-\d\d\d\d$/', check_out => '/^[0-1]\d-[0-3]\d-\d\d\d\d$/', }, template => { type => 'TT2', template => 'visitor_edit.tt2', variable => 'form', engine => { INCLUDE_PATH => '/inetpub/wwwroot/' . 'lzprotocols.partners.org/ +' . 'socialservices/src', }, }, );
In my debugging portion, the hash prints with the proper data from the database, however the values do not get loaded as defaults on the form. If I just "hard code" some random values into the hash rather than do the database lookup, the values do get passed through as defaults to the form. Is there some difference between doing a database lookup to populate a hash and hardcoding one directly? Does anyone have any experience like this with CGI::Formbuilder? Also, this is perl 5.8 on Apache 2.x running on a win32 machine. Thank you.

Replies are listed 'Best First'.
Re: Setting Default Values using CGI::FormBuilder
by naikonta (Curate) on Jun 21, 2007 at 20:25 UTC
    This may be the worng place to ask a question, but I think I am right
    PerlMonks is just the right place to ask, as well as FB website (they have official mailing list at fbusers-subscribe@formbuilder.org), or, perhaps, CPAN Forum (though it seems very quiet).
    print "Content-type: text/html\n\n";
    Oh come on, you're using CGI but left out its header() method? :-)
    # for debugging to se if the hash was populated
    I think you'd be better to use Data::Dumper or other similiar modules from CPAN.
    In my debugging portion, the hash prints with the proper data from the database, however the values do not get loaded as defaults on the form.
    Unfortunately, you didn't show the (sample) code of what your debugging portion threw out. However, I emulate this by reading the data from external file and it works just fine. The two approaching might be incompatible in some way, but I take the essence that the data to be assigned is external.
    $ cat sample.txt a 5 b 6 c 7 d 8
    cfb.pl
    #!/usr/bin/perl use strict; use warnings; use CGI::FormBuilder; my @fields = qw(a b c d); my %values = get_values(shift || ''); my $form = CGI::FormBuilder->new(fields => \@fields, values => \%value +s); print "$_ => ", $form->field($_), "\n" for @fields; sub get_values { my $way = shift; return (a => 1, b => 2, c => 3, d => 4) unless $way; my %fetched; if ($way eq 'file') { open my $fh, '<', 'sample.txt' or die "open failed: $!\n"; while (<$fh>) { chomp; my($key, $value) = split; $fetched{$key} = $value; } close $fh; } # additional if?? return %fetched; }
    Output:
    $ perl cfb.pl a => 1 b => 2 c => 3 d => 4 $ perl cfb.pl file a => 5 b => 6 c => 7 d => 8

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!