rincewind666
asked on
Require fields over several cgi scripts
I have several web form scripts, each one calling the next one in sequence. I need to make some variables into required fields such as:
print_error("You must enter your email address") unless $email;
For example, I would need to make "Email" a required field in the first form.
I am using hidden fields in the "here-doc" code to carry the values over to subsequent forms but this code doesn't work here. What can I do? Your help would be greatly appreciated.
I am a novice so a easy-to-understand answer is necessary :-)
######################### FIRST SCRIPT #######################
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header;
print <<HTMLCODE;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" background="" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<FORM ACTION="http://demo.com/cgi-bin/test/form2.cgi" METHOD="POST">
<TABLE>
<TR>
<TD>Name: <INPUT name=name></TD></TR>
<TR>
<TD>Email: <INPUT name=email></TD></TR>
<TR>
<TD><INPUT type=submit value=Submit>
<INPUT type=reset value=Reset></TD></TR></TA BLE></P>
</BODY>
</HTML>
HTMLCODE
sub print_error {
my $message = shift;
print $q->header;
print <<ERRORCODE;
<html>
<head><title>$message</tit le></head>
<body>
<p align="center"><font color="#000000" face="Arial"><big><strong> Processing Error</strong></big></font ></p>
<p align="left"><font face="Arial" color="#000080"><strong>$m essage</st rong></fon t></p>
<p align="left"><font face="Arial" color="#000080"><strong>Pl ease click your browser's Back button and try again.</strong></font></p>
</body>
</html>
ERRORCODE
exit;
}
########################## #### SECOND SCRIPT #####################
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $name = $q->escapeHTML($q->param(' name'));
my $email = $q->escapeHTML($q->param(' email'));
print $q->header;
print <<HTMLCODE;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" background="" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<FORM ACTION="http://demo.com/cgi-bin/test/form3.cgi" METHOD="POST">
<input type="hidden" name="name" value="$name" />
<input type="hidden" name="email" value="$email" />
<TABLE>
<TR>
<TD>Age: <INPUT name=age></TD></TR>
<TR>
<TD>Country: <INPUT name=country></TD></TR>
<TR>
<TD><INPUT type=submit value=Submit>
<INPUT type=reset value=Reset></TD></TR></TA BLE></P>
</BODY>
</HTML>
HTMLCODE
sub print_error {
my $message = shift;
print $q->header;
print <<ERRORCODE;
<html>
<head><title>$message</tit le></head>
<body>
<p align="center"><font color="#000000" face="Arial"><big><strong> Processing Error</strong></big></font ></p>
<p align="left"><font face="Arial" color="#000080"><strong>$m essage</st rong></fon t></p>
<p align="left"><font face="Arial" color="#000080"><strong>Pl ease click your browser's Back button and try again.</strong></font></p>
</body>
</html>
ERRORCODE
exit;
}
print_error("You must enter your email address") unless $email;
For example, I would need to make "Email" a required field in the first form.
I am using hidden fields in the "here-doc" code to carry the values over to subsequent forms but this code doesn't work here. What can I do? Your help would be greatly appreciated.
I am a novice so a easy-to-understand answer is necessary :-)
######################### FIRST SCRIPT #######################
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header;
print <<HTMLCODE;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" background="" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<FORM ACTION="http://demo.com/cgi-bin/test/form2.cgi" METHOD="POST">
<TABLE>
<TR>
<TD>Name: <INPUT name=name></TD></TR>
<TR>
<TD>Email: <INPUT name=email></TD></TR>
<TR>
<TD><INPUT type=submit value=Submit>
<INPUT type=reset value=Reset></TD></TR></TA
</BODY>
</HTML>
HTMLCODE
sub print_error {
my $message = shift;
print $q->header;
print <<ERRORCODE;
<html>
<head><title>$message</tit
<body>
<p align="center"><font color="#000000" face="Arial"><big><strong>
<p align="left"><font face="Arial" color="#000080"><strong>$m
<p align="left"><font face="Arial" color="#000080"><strong>Pl
</body>
</html>
ERRORCODE
exit;
}
##########################
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $name = $q->escapeHTML($q->param('
my $email = $q->escapeHTML($q->param('
print $q->header;
print <<HTMLCODE;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" background="" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<FORM ACTION="http://demo.com/cgi-bin/test/form3.cgi" METHOD="POST">
<input type="hidden" name="name" value="$name" />
<input type="hidden" name="email" value="$email" />
<TABLE>
<TR>
<TD>Age: <INPUT name=age></TD></TR>
<TR>
<TD>Country: <INPUT name=country></TD></TR>
<TR>
<TD><INPUT type=submit value=Submit>
<INPUT type=reset value=Reset></TD></TR></TA
</BODY>
</HTML>
HTMLCODE
sub print_error {
my $message = shift;
print $q->header;
print <<ERRORCODE;
<html>
<head><title>$message</tit
<body>
<p align="center"><font color="#000000" face="Arial"><big><strong>
<p align="left"><font face="Arial" color="#000080"><strong>$m
<p align="left"><font face="Arial" color="#000080"><strong>Pl
</body>
</html>
ERRORCODE
exit;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
~= should be =~
thanks for correting my typos, ozo
ASKER
It is not being redirected. The next form is displayed with this message on the bottom:
Content-Type: text/html; charset=ISO-8859-1 Status: 302 Moved Location: http://demo.com/form1-page.html
Content-Type: text/html; charset=ISO-8859-1 Status: 302 Moved Location: http://demo.com/form1-page.html
damn, had a lazy day ... sorry.
Please remove in the if
print $q->header;
Please remove in the if
print $q->header;
ASKER
I've changed this to:
if ("$email" =~ /^\s*$/) {
print $q->redirect('http://demo.com/form1-page.html');
exit(0);
}
Now the next form is still being displayed with this message on the bottom:
Status: 302 Moved Location: http://demo.com/form1-page.html
if ("$email" =~ /^\s*$/) {
print $q->redirect('http://demo.com/form1-page.html');
exit(0);
}
Now the next form is still being displayed with this message on the bottom:
Status: 302 Moved Location: http://demo.com/form1-page.html
do you have another
print $q->header;
right before the if?
If so, move it behind the if
print $q->header;
right before the if?
If so, move it behind the if
ASKER
No. Still getting the same error. Here is the current code:
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $name = $q->escapeHTML($q->param(' name'));
my $email = $q->escapeHTML($q->param(' email'));
#print $q->header;
print <<HTMLCODE;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" background="" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<FORM ACTION="http://demo.com/cgi-bin/test/form3.cgi" METHOD="POST">
<input type="hidden" name="name" value="$name" />
<input type="hidden" name="email" value="$email" />
<TABLE>
<TR>
<TD>Age: <INPUT name=age></TD></TR>
<TR>
<TD>Country: <INPUT name=country></TD></TR>
<TR>
<TD><INPUT type=submit value=Submit>
<INPUT type=reset value=Reset></TD></TR></TA BLE></P>
</BODY>
</HTML>
HTMLCODE
if ("$email" =~ /^\s*$/) {
print $q->redirect('http://demo.com/form1-page.html');
exit(0);
}
print $q->header;
sub print_error {
my $message = shift;
print $q->header;
print <<ERRORCODE;
<html>
<head><title>$message</tit le></head>
<body>
<p align="center"><font color="#0000000" face="Arial"><big><strong> Processing Error</strong></big></font ></p>
<p align="left"><font face="Arial" color="#000080"><strong>$m essage</st rong></fon t></p>
<p align="left"><font face="Arial" color="#000080"><strong>Pl ease click your browser's Back button and try again.</strong></font></p>
</body>
</html>
ERRORCODE
exit;
}
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $name = $q->escapeHTML($q->param('
my $email = $q->escapeHTML($q->param('
#print $q->header;
print <<HTMLCODE;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" background="" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<FORM ACTION="http://demo.com/cgi-bin/test/form3.cgi" METHOD="POST">
<input type="hidden" name="name" value="$name" />
<input type="hidden" name="email" value="$email" />
<TABLE>
<TR>
<TD>Age: <INPUT name=age></TD></TR>
<TR>
<TD>Country: <INPUT name=country></TD></TR>
<TR>
<TD><INPUT type=submit value=Submit>
<INPUT type=reset value=Reset></TD></TR></TA
</BODY>
</HTML>
HTMLCODE
if ("$email" =~ /^\s*$/) {
print $q->redirect('http://demo.com/form1-page.html');
exit(0);
}
print $q->header;
sub print_error {
my $message = shift;
print $q->header;
print <<ERRORCODE;
<html>
<head><title>$message</tit
<body>
<p align="center"><font color="#0000000" face="Arial"><big><strong>
<p align="left"><font face="Arial" color="#000080"><strong>$m
<p align="left"><font face="Arial" color="#000080"><strong>Pl
</body>
</html>
ERRORCODE
exit;
}
no, no, please read my comments!
...
my $email = $q->escapeHTML($q->param(' email'));
if ("$email" =~ /^\s*$/) {
print $q->redirect('http://demo.com/form1-page.html');
exit(0);
}
print $q->header;
print <<HTMLCODE;
...
and always exit your cgi with
exit( 0 );
otherwise you get a server 500 error ..
...
my $email = $q->escapeHTML($q->param('
if ("$email" =~ /^\s*$/) {
print $q->redirect('http://demo.com/form1-page.html');
exit(0);
}
print $q->header;
print <<HTMLCODE;
...
and always exit your cgi with
exit( 0 );
otherwise you get a server 500 error ..
ahoffman.
Please explain your rationale behind:
and always exit your cgi with
exit( 0 );
Please explain your rationale behind:
and always exit your cgi with
exit( 0 );
Tintin, the CGI defines that each called cgi must return 0 on success, otherwise the web-server should/must return a server 500 error.
A perl script returns 0 by default, of the last command right before the final return/exit was successful, so it can be omitted.
Writing exit(0) is clean programing, beside you never know what happens on the system ..
A perl script returns 0 by default, of the last command right before the final return/exit was successful, so it can be omitted.
Writing exit(0) is clean programing, beside you never know what happens on the system ..
A lot of people (myself included) would argue that adding a exit(0) in a Perl (or shell script) for that matter at the end of the script is totally redundant and unless your last command has any error checking, it could mean a misleading result.
ASKER
Thanks for your help. Much appreciated.
it might be redundant, but it's bad, very bad, programming style to leave it all to the interpreter's (perl) implementation
and in case of errors, you don't know what happend.
Think secure ;-)
and in case of errors, you don't know what happend.
Think secure ;-)
ahoffman.
I'll agree to disagree on this one.
Here's an example of it giving misleading results:
#!/bin/sh
sdf
exit 0
I'll agree to disagree on this one.
Here's an example of it giving misleading results:
#!/bin/sh
sdf
exit 0
.. or this one:
#!/bin/sh
/bin/false
exit 0
;-)
rincewind666, hope you got a valuable solution beside some off-topic chat ..
#!/bin/sh
/bin/false
exit 0
;-)
rincewind666, hope you got a valuable solution beside some off-topic chat ..
ASKER
Software error:
syntax error at form2.cgi line 39, near ""$email" ~"
syntax error at form2.cgi line 43, near "}"
Execution of form2.cgi aborted due to compilation errors.
This is the amended script:
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $name = $q->escapeHTML($q->param('
my $email = $q->escapeHTML($q->param('
print $q->header;
print <<HTMLCODE;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" background="" text="#000000" link="#000000" vlink="#000000" alink="#000000">
<FORM ACTION="http://demo.com/cgi-bin/test/form3.cgi" METHOD="POST">
<input type="hidden" name="name" value="$name" />
<input type="hidden" name="email" value="$email" />
<TABLE>
<TR>
<TD>Age: <INPUT name=age></TD></TR>
<TR>
<TD>Country: <INPUT name=country></TD></TR>
<TR>
<TD><INPUT type=submit value=Submit>
<INPUT type=reset value=Reset></TD></TR></TA
</BODY>
</HTML>
HTMLCODE
################ AMENDED HERE
if ("$email" ~= /^\s*$/) {
print $q->header;
print $q->redirect('http://demo.com/form1-page.html');
exit(0);
}
###############
sub print_error {
my $message = shift;
print $q->header;
print <<ERRORCODE;
<html>
<head><title>$message</tit
<body>
<p align="center"><font color="#000000" face="Arial"><big><strong>
<p align="left"><font face="Arial" color="#000080"><strong>$m
<p align="left"><font face="Arial" color="#000080"><strong>Pl
</body>
</html>
ERRORCODE
exit;
}