perlnewbie-newbie has asked for the wisdom of the Perl Monks concerning the following question:
Here's a rundown of my code:
What I'm getting now is that the form doesnt remember the user's input data into all the form fields. The error arrows and messages show up correctly, just that each time I submit the form with errors, the input data will be erased which is surely unuser-friendly. I guess the general idea of doing it is: retrieve all the input data variables (ie $name, $username etc) and put them back into the form fields, all these in &printHTML subroutine.#!/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 = "<a href=\"javascript:alert('Please enter a name.')\ +"><img src=\"/pics/generic/error.gif\" border=\"0\"></a>"; } . . . } ### Subroutine that generates the page with error indications sub printHTML { print "Content-Type: text/html\n\n"; $filename = "signup.htm"; open (INFILE,$filename); while (<INFILE>) { #alot of substitutions here...eg: if ($errname='1') { s/<input type="hidden" name="hiddenField" value= +"~error1~">/$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 l +ocations & 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 (<INFILE>) { print $_; } close (INFILE); }
But the exact way of doing that I'm not sure. I'd greatly appreciate any help here. Thanx in advance!
Edit by tye to add <p>,<readmore>.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Remembering form data without cgi.pm
by Juerd (Abbot) on Apr 11, 2002 at 07:44 UTC | |
|
Re: Remembering form data without cgi.pm
by George_Sherston (Vicar) on Apr 11, 2002 at 09:11 UTC | |
|
Re: Remembering form data without cgi.pm
by derby (Abbot) on Apr 11, 2002 at 11:56 UTC | |
|
Re: Remembering form data without cgi.pm
by perlnewbie-newbie (Initiate) on Apr 11, 2002 at 16:40 UTC | |
|