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.

In reply to Setting Default Values using CGI::FormBuilder by nakajima

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.