#!/usr/bin/perl use CGI::Carp "fatalsToBrowser"; use DBI; ### Subroutine that parses the form. sub parse_form { # Get the input. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Splits the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $FORM{$name} = $value; } ### Subroutine that catches form variables sub get_variables { $name = $FORM{'name'}; $username = $FORM{'username'}; $email = $FORM{'email'}; #other variables follow . . . } ### Subroutine that validates form submission sub validate_form { #validation routines, eg: if ($name eq "") { $error = '1'; $errname = '1'; $error1 = ""; } . . . } ### Subroutine that generates the page with error indications sub printHTML { print "Content-Type: text/html\n\n"; $filename = "signup.htm"; open (INFILE,$filename); while () { #alot of substitutions here...eg: if ($errname='1') { s//$error1/g; } . . . print $_; } close(INFILE); } ########### Main body #form submitted with arguments, so process it if ($ENV{'CONTENT_LENGTH'}) { &parse_form; &get_variables; &validate_form; #if no error, insert into db if ($error eq '0') { #insert form data into db } #if errors, substitute hidden fields with arrows indicating error locations & messages else { &printHTML; } } #first time in: no arguments in form, so present empty form else { print "Content-Type: text/html\n\n"; $filename = "signup.htm"; open (INFILE,$filename); while () { print $_; } close (INFILE); }