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

Hello and thanks for taking a look at this!

I am a new perl programmer using CGI.pm to create my application.

I have a subroutine defaultNewInvest() that creates a form that users can input some data. (The form has been truncated.) I would like to be able to search the database and retrieve the information INTO THE SAME FORM so that changes can be made.

I have no trouble creating the form, inserting the data into an Oracle database, nor retrieving the names into a list. If I click on the name, I would like to be taken back to the original form with the information filled in.

How do I query the database and pass all the information to defaultNewInvest() so that the information that is currently in the database can be viewed and/or edited???

I have searched the web and can not find any examples of this. I have seen HTML::Template but did not know if that was the best was to proceed. If anything is not clear, please ask. I am desperate for a solution.

Thanks in advance. I am including the code for creating my form. Maybe that will help in figuring out what I am not understanding.

PS. I have not included all hte subroutines. I merely included the if-elsif-else structure so that one can see the flow. Thanks again.

#!c:/perl/bin/perl.exe -w use strict; use DBI; use CGI; use vars qw($CGI $DBH $dbsource $schema $user $psswd $DEBUG $cookie); ($dbsource, $schema, $user, $psswd) = ($ENV{DB_SOURCE}, $ENV{SCHEMA}, +$ENV{USER}, $ENV{PSSWD}); $CGI = new CGI; $DBH=DBI->connect($dbsource,$user,$psswd,{RaiseError =>1, AutoCommit = +>0}) || die "Database connection not made: $DBI::errstr"; if ( $CGI->param("saveNewInvestBtn") ) { insertNewInvestigator($CGI, $DBH); } elsif ( $CGI->param("peopleSearchBtn") ) { peopleSearchResults($CGI, $DBH); } elsif ($CGI->param("keywords") eq "newInvest" ) { defaultNewInvestigator($CGI); } else { mainPage($CGI); } sub defaultNewInvest { my $q = shift; print $q->header(); print $q->start_html(-title=>'Primary Investigator Information'); print $q->h1("Primary Investigator Information"); print $q->start_form(-action=>"investigator.pl", -method=>"post", +-name=>"form1", -onSubmit=>"return checkInvestForm();"); print$q->h3("Address Information"); print $q->table( {-border=>0}, $q->Tr([ $q->td( ["Salutation*:", $q->popup_menu(-name=>"inves +t_salutation", -values=>["Dr.", "Mr.", "Mrs.", "Ms."])."   +Degree:   ".$q->textfield(-name=>"invest_degree")]), $q->td( ["First Name*:", $q->textfield(-name=>"invest +_fname", -size=>40)] ), $q->td( ["Mid. Name:", $q->textfield(-name=>"invest_m +name", -size=>40)]), $q->td( ["Last Name*:", $q->textfield(-name=>"invest_ +lname", -size=>40)] ), $q->td( ["Title:", $q->textfield(-name=>"invest_title +", -size=>40)]) ]) ); print $q->submit( -name=>"saveNewInvestBtn", -value=>"Save", class +=>"buttonstyle" ); print $q->end_form(); print $q->end_html(); }

Replies are listed 'Best First'.
Re: Populating a form with data from a DB
by sandfly (Beadle) on Sep 08, 2004 at 22:38 UTC
    If you want to use the CGI for this, you have two options:
    1. Set the value of the parameter using
    $q->param("invest_fname", "Joe");

    or
    2. Use -default and -override when you print the control:
    $q->textfield( -name=>"invest_lname", -default=>"Soap", -override=>1 );
    Sometimes I show pages containing several forms, so I prefer the latter approach.
    (No code was tested in the writing of this answer.)

Re: Populating a form with data from a DB
by joecamel (Hermit) on Sep 08, 2004 at 22:18 UTC
    I don't use CGI.pm to generate HTML, but I think you can add -value to your args. Something like this:
    sub getInvest { my $investID = shift || return {}; # grab row from database with fethrow_hashref() return \%invest; } sub defaultNewInvest { my $q = shift; my $fillIn = getInvest($q->param('investID')); # ... $q->td( ["Title:", $q->textfield(-name=>"invest_title", -size=>40, +-value=>$fillIn->{'invest_title'} )]) ]); }
    Using a templating solution like HTML::Template is a good way to handle this type of thing, because you can usually just pass the hash you get from your database call right into the template's param() method.

    joecamel
Re: Populating a form with data from a DB
by cLive ;-) (Prior) on Sep 09, 2004 at 09:28 UTC

    Please read this.

    cLive ;-)

Re: Populating a form with data from a DB
by Anonymous Monk on Sep 09, 2004 at 18:49 UTC
    Thanks for your suggetions. I am trying out the first two suggestions. They are fairly similar, I believe. I went to the link suggested in the 3rd post and did not find what I was looking for. I will post later on if I have any success. Debbie