homepage Welcome to WebmasterWorld Guest from 54.167.174.90
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Redirection after form submission
joharran

5+ Year Member



 
Msg#: 3863859 posted 9:38 pm on Mar 5, 2009 (gmt 0)

I'm using perl to validate a form and save the results. That part works fine. Here's a scenario to demonstrate my issue:

Page #1 contains a link to page #2. Page #2 contains a form which runs my script upon submission.

If the data doesn't validate, I would like to reload page #2 with an error message. (I can handle adding the message, I just need a way to trigger the page reload)

If the data does validate, I would like to force the browser to backup and display page #1. I do not wish to add a new iteration of page #1 after page #2, I want to go back to the previous page.

I considered using javascript to submit the form, but I cannot figure out a way to inform javascript whether the validation passed/failed.

Does anybody have any suggestions?

 

krugs

5+ Year Member



 
Msg#: 3863859 posted 10:42 pm on Mar 5, 2009 (gmt 0)

If you are using static pages this is not easy, but it is pretty easy if your pages are created dynamically by a script. Perl can't force a browser to do anything though, so I am not sure if you are asking a javascript question or not. To me it sounds like if the form data validates you should just redirect to page1, why do you need to go back in history to page1? which perl can't do anyway.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3863859 posted 8:19 pm on Mar 7, 2009 (gmt 0)

Welcome aboard joharran!

Here's an "outline" that might work for you. Instead of having a "page #2", just put it all in your script. This allows you to dynamically populate the fields on error. So page one, you have

<a href="join.cgi">join now</a>

And the "outline" would be something like this:

%data = &read_parse;

Whatever read parse routine you use, be sure to cleanse the data at the same time, probably a topic for another thread.

With all input in key/value pairs in %data, look for anything that equates to "form submitted." You could use, say, a hidden field right in the form,

<input type="hidden" name="fs" value="1">

Alternatively, use any of the form fields to determine if it's submitted.

So your program starts off with

$errors = '';
if ($data{'fs'}) { &attempt_to_process; }
## or, "if ($data{'email'}," or any other form field
else { &display_form; }

&display_form does just what your page #2 does, outputs the form, but if there is user-submitted data, you populate the fields. If there is any error in &attempt_to_process, you output those errors above the form. Something like

sub display_form {

my ($form);

if ($errors ne '') {
$form .= qq<p style="color:#ff0000; font-weight:700;">Errors were encountered in processing this form:</p><ul>$errors</ul>;
}

$form .= qq
<form method="post" action="join.cgi">
<input type="hidden" name="fs" value="1">
<label for="email">Email:</label> <input type="text" name="email" id="email" value="$data{'email'}">
...
</form>
;

print "content-type: text/html\n\n";
print $form;
exit 0;
}

So the first time it loads,
- There are no errors, so just the form displays.
- There is no "$data{'email'}" (or other submitted data) so the field values are blank. But if it gets submitted, and there's a value there, it will populate whatever they entered.

In attempt_to_process, you'd do your data checks with a third sub that compiles a list of errors (note the "<ul>" in the error string.) If no errors are encountered, you process the form. Some typical error checks:
- required fields blank?
- valid email address?
- malicious data found?

sub attempt_to_process {

$errors = &check_data;

if ($errors ne '') { &display_form; }

else {
&register_in_database;
&send_email_confirmation;
&thank_you;
}

}

sub check_data {
my ($dataError,$v,%requireds);
$dataError = '';
%requireds = (
'email' => 'email address',
'pwd' => 'password',
'fn' => 'first name',
'ln' => 'last name'
);
foreach $v (keys %requireds) {
unless ($data{$v} =~ /\w+/) {
$dataError .= qq<li>The $requireds{$v} field is required.</li>;
}
## You'd also add valid email checks here, check for malicious data, etc.
return $dataError;
}

If you really want to redirect back to page 1, that's fine, &thank_you can be a simple

sub thank_you {
print "Location:page1.html\n\n";
}

but why do that when you already have all the data in hand . . .

sub thank_you {

my ($out);

$out = qq
<p>Thank you for joining $data{'fn'}! An email confirmation has been sent to $data{'email'}.</p>
...... (you could emulate all submitted data here, or display help files . . whatever . . . . )
<p><a href="/">Main Page</a> \ <a href="page1.html">Page One</a></p>
;

print "content-type: text/html\n\n";
print $out;
exit 0;
}

There's a lot of blanks to fill in here, but it's a fun journey, good luck!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved