nakajima has asked for the wisdom of the Perl Monks concerning the following question:
Here is the script that I am trying to make work.#!/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' );
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.#!/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', }, }, );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Setting Default Values using CGI::FormBuilder
by naikonta (Curate) on Jun 21, 2007 at 20:25 UTC |