Home: Perl Programming Help: Advanced:
Form Processing


Jul 13, 2000, 10:13 PM

Views: 4729
Form Processing

I am in need of a form script that will allow for the collection
of 5 items..
(1) Comment field
(4) Hidden fields (Identifying information within the page itself.)
It will:
- Transfer the info to an Oracle7 database via TCP/IP. (With
a time and Date stamp)
- Redirect the submitter to a confirmation page.
How can this be done? I have experience appending flat-file databases only. Thanks in advance.

David Durose

User / Moderator

Jul 14, 2000, 1:16 PM

Views: 4729
Re: Form Processing

See the CGI, DBI and DBD::Oracle modules.

User / Moderator

Jul 14, 2000, 4:39 PM

Views: 4729
Re: Form Processing

Per my response to your email a very simplistic database read/writer ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w

## --/ SCRIPT CONFIGURATION /------------------------------- ##
## $dsn is the data source name we need to access the db,
## and takes the form of dbi:DRIVER:DATABASE where DRIVER
## is derived from the DBD::DRIVER modules and DATABASE
## is the name (and host if nedded) of the db we want.
## Sample DSNs are dbi:CSV:f_dir=/tmp/my_books (a CSV db),
## dbi:mysql:database=my_books;host= (a MySQL db
## hosted a different server), and
## dbi:Oracle:my_books (Oracle).
my $dsn = 'dbi:CSV:f_dir=/tmp/mybooks';

## Username and password needed to authenticate (if any).
my $user = "";
my $pass = "";

## Any extra parameters to pass to control various script
## <-> db behaviours. See the DBI and DBD::DRIVER man
## pages for all possibilities, but we're going with
## automatically committing all our queries, and handle
## errors ourselves.
my %attr = ( AutoCommit => 1, PrintError => 0, RaiseError => 0 );

## --/ MODULES WE NEED /------------------------------------ ##
use CGI; # ... for forms processing
use CGI::Carp 'fatalsToBrowser'; # ... errors to the browser
use DBI; # ... for database interaction

use strict; # ... cause we're prim & proper

## --/ DA PROGRAM! /---------------------------------------- ##

# Unbuffer out output so that genuine output has a chance to
# reach the browser before errors (if any) do.

# Create a CGI object. I generally don't go with the OO
# method of CGI.pm, but in this case I think'll it help
# distinguish what module is handling what. ;^)
my $cgi = CGI->new;

# Connect to the database ...
my $db = DBI->connect( $dsn, $user, $pass, \%attr )
or die( "Can't connect because ", DBI->errstr );

# Output HTTP headers, and the HTML <head>'er.
print $cgi->header,
$cgi->start_html( -title => 'form2db example' );

# Working backwards here.

# If someone has submitted a form with both the title and
# author fields filled out, and the fields contain at least
# one alphanumeric character
if ( $cgi->param('title') && $cgi->param('title') =~ /\w/ &&
$cgi->param('author') && $cgi->param('author') =~ /\w/ ) {

# Prepare our query ...
my $stmt = $db->prepare( "INSERT INTO book
(title,author) VALUES (?,?)" )
or die( "Can't prep INSERT because ", $db->errstr );

# And then run it, inserting the data from the form.
$stmt->execute( $cgi->param('title'), $cgi->param('author') )
or die( "Can't exec INSERT because ", $stmt->errstr );

# Clean up after ourselves.

# If they submitted the form w/o entering data for both
# fields, tell them off ...
} elsif ( $cgi->param ) {
print $cgi->p( "You must fill in BOTH fields" );

# Regardless of what the visitor has just done (or not done)
# or whether they were successful, spit out the existing
# contents of the the database.
print $cgi->start_form,
$cgi->start_table( { -border => 1 } ),
$cgi->Tr( $cgi->th( [ "Title", "Author" ] ) );

# Yup, we prepare another query!
my $stmt = $db->prepare( "SELECT title,author FROM book" )
or die( "Can't prep SELECT because ", $db->errstr );

# ... run it ...
$stmt->execute or die( "Can't exec SELECT because ", $db->errstr );

my $books = 0;

# ... and then iterate over the results it returns ...
while( my $row = $stmt->fetchrow_arrayref ) {
print $cgi->Tr( $cgi->td( $row ) );

# Finally, include an input form so they can add more
# entries themselves.
print $cgi->Tr(
[ $cgi->textfield( -name => 'title' ),
$cgi->textfield( -name => 'author' ), ],
$cgi->submit( -value => 'Add book!' ),
$cgi->p( "There are ", ($books ? $books : "no"),
" books in the db." ),

[This message has been edited by Kanji (edited 07-14-2000).]


Jul 15, 2000, 12:02 PM

Views: 4729
Re: Form Processing

Will this script work for an Oracle 7 database, though? Thanks for your help.

User / Moderator

Jul 15, 2000, 12:27 PM

Views: 4729
Re: Form Processing

<UL TYPE=SQUARE><LI>Read my comments on DSNs within the script.<LI>Find an appropriate database driver ( DBD:: ) to work with DBI.<LI>Read it's README and documentation.</UL>


Jul 15, 2000, 4:09 PM

Views: 4729
Re: Form Processing

Thanks for your help. I downloaded DBD::Oracle and it looks like I need to use Oracle.pm. Does that sound right to you? What else do I need? I should just require it at the top of my form processor script?



Jul 15, 2000, 4:12 PM

Views: 4729
Re: Form Processing

What can you tell me about Oraperl? Is it necessary to use if I just want to develop a form processor script that will connect to an Oracle 7 db and input some form information submitted by the user?


User / Moderator

Jul 15, 2000, 8:10 PM

Views: 4729
Re: Form Processing

DBD::Oracle ( aka DBD/Oracle.pm ) is useless without DBI.pm, so you'll need to install that along with CGI.pm ( if you don't already have it ).

DBD::Oracle won't be called directly: that's done via the DSN you pass to the DBI module (ie, dbi:Oracle:database_name ), but you'll need to use or require both the DBI and CGI modules at the top of your script.

Oraperl was obsoleted by DBI and DBD::Oracle, so no, it is not necessary if you use the DBI method instead.

See my example.

It's heavily commented and fully functional. If you've paid attention to anything that's been said, you'll know that to get it running with an Oracle would only require changing one line in the entire script ( hint: my $dsn = 'dbi:..., you fill in the rest ).