CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Form Processing



Jul 13, 2000, 10:13 PM

Post #1 of 8 (2016 views)
Form Processing Can't Post

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

Post #2 of 8 (2016 views)
Re: Form Processing [In reply to] Can't Post

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

User / Moderator

Jul 14, 2000, 4:39 PM

Post #3 of 8 (2016 views)
Re: Form Processing [In reply to] Can't Post

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, 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

Post #4 of 8 (2016 views)
Re: Form Processing [In reply to] Can't Post

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

User / Moderator

Jul 15, 2000, 12:27 PM

Post #5 of 8 (2016 views)
Re: Form Processing [In reply to] Can't Post

<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

Post #6 of 8 (2016 views)
Re: Form Processing [In reply to] Can't Post

Thanks for your help. I downloaded DBD::Oracle and it looks like I need to use 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

Post #7 of 8 (2016 views)
Re: Form Processing [In reply to] Can't Post

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

Post #8 of 8 (2016 views)
Re: Form Processing [In reply to] Can't Post

DBD::Oracle ( aka DBD/ ) is useless without, so you'll need to install that along with ( 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 ).


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives