+#!/usr/bin/perl -Tw
+# $Id: persons.cgi,v 1.1 1999-01-27 09:55:05 ivan Exp $
+# Copyright (c) 1999 Ivan Kohler. All rights reserved.
+# This program is free software; you can redistribute it and/or modify it under
+# the same terms as perl itself
+
+use strict;
+use vars qw ( $data_source $user $password $table @fields
+ $cgi $dbh
+ );
+use subs qw( print_form );
+use CGI;
+use CGI::Carp qw(fatalsToBrowser);
+use DBI;
+
+$data_source = "DBI:mysql:test";
+$user = "ivan";
+$password = "";
+
+$table = "PERSONS";
+@fields = qw( ID NAME EMAIL AFFILIATION );
+
+$cgi = new CGI;
+
+$dbh = DBI->connect( $data_source, $user, $password )
+ or die "Cannot connect: ". $DBI::errstr;
+
+unless ( $cgi->param('magic') ) { #first time through
+
+ my $sth = $dbh->do( "SELECT * FROM PERSONS" ) or die $dbh->errstr;
+
+ print $cgi->header,
+ $cgi->start_html('Person listing'),
+ $cgi->h1('Person listing'),
+ ;
+
+ unless ( $sth eq '0E0' ) {
+
+ print $cgi->start_table,
+ $cgi->tr(
+ map {
+ $cgi->th($_)
+ } @{$sth->{NAME}}
+ ),
+ map {
+ $cgi->tr(
+ map {
+ $cgi->td( $_ )
+ } @{ $_ }
+ )
+ } @{ $sth->fetchall_arrayref },
+ $cgi->end_table,
+ ;
+ }
+
+ $cgi->param('magic', 'new_form');
+ print '<P><A HREF="', $cgi->self_url, '">Add new person</A>';
+ print $cgi->end_html;
+
+ exit;
+
+} elsif ( $cgi->param('magic') eq 'new_form' ) {
+ $cgi->param('ID', 0);
+ $cgi->param('magic', 'process_form');
+ &print_form( $cgi, "Add person" );
+ exit;
+} elsif ( $cgi->param('magic') eq 'process_form' ) {
+ print $cgi->header,
+ "process form stub"
+ ;
+ exit;
+}
+
+sub print_form {
+ my $cgi = shift;
+ my $action = shift;
+ print $cgi->header,
+ $cgi->start_html($action),
+ $cgi->h1($action),
+ $cgi->start_form,
+ $cgi->hidden( -name => 'ID' ),
+ "Name: ", $cgi->textfield( -name => 'NAME' ), "<BR>",
+ "Email: ", $cgi->textfield( -name => 'EMAIL' ), "<BR>",
+ "Affiliation: ", $cgi->textfield( -name => 'AFFILIATION' ), "<BR>",
+ $cgi->hidden( -name => 'magic'),
+ $cgi->submit('Submit'),
+ $cgi->end_form,
+ $cgi->end_html;
+ ;
+}