Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Populating HTML form fields from Database

by peacemaker1820 (Pilgrim)
on Jul 02, 2002 at 13:52 UTC ( #178849=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks! I need to populate my form fields with some information that i am taking from the database. However, I need it to work as such: when I am in my form it has to go to my perl file, get some info and populate the fields in the SAME form with out changing the page.

Thanks Brothers.
  • Comment on Populating HTML form fields from Database

Replies are listed 'Best First'.
(jeffa) Re: Populating HTML form fields from Database
by jeffa (Bishop) on Jul 02, 2002 at 14:30 UTC
    This should get you started. You have stated in the CB that this is a homework assignment - i always recommend that you talk to your teacher/instructor/professor instead of handing them the code we provide, because you are not going to have our help when the exam rolls around. ;)
    #!/usr/bin/perl -Tw use strict; use CGI qw(:standard); use DBI; # connect to the database my $dbh = DBI->connect( qw(DBI:mysql:database:host user pass), ) or die $DBI::errstr; # prepare the SQL statement my $sth = $dbh->prepare(' select foo from baz where bar = ? ') or die "prepare failed"; # feed the SQL statement the criteria $sth->execute(8) or die "execute failed"; # get an array of the fetched row - since # we are only grabbing one field ... my @row = $sth->fetchrow_array(); # we will store it in a scalar my $field = $row[0]; # print the HTML print header, start_html,start_form, textfield(-name=>'foo',-value=>$field), end_form,end_html, # clean up $sth->finish; $dbh->disconnect;
    As you can see, this is just an example. It is up to you to make this work for your problem. If you have more questions, please show us the code you have written so far. Good luck. :)

    (update - added shebang line to include taint option)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Populating HTML form fields from Database
by schumi (Hermit) on Jul 02, 2002 at 14:30 UTC
    Hi.

    Apart from use-ing strict and warnings - which you certainly do, I'm sure - you should look at CGI and DBI.

    As for populating the fields in the same form, it seems to me that you'll have to put the script into the file with the form and then have it call itself.

    --cs

    There are nights when the wolves are silent and only the moon howls. - George Carlin

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://178849]
Approved by schumi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2020-11-24 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?