mkarmali
04-20-2008, 11:17 AM
Hi:
I am including below a cgi/perl script. I am trying to validate fields in a form.
I am trying to do it in 2 ways. One is to have the script print out a javascript function in my HTML page, and the other is to do it with a Cgi function.
Neither of the two work and I cannot see why. Would someone please be able to go over my script and tell me if they see anything wrong that I am doing.
Many thanks.
m. karmali
#!/usr/local/bin/perl -w
use CGI;
my $query = new CGI;
&print_page_start;
if ($query->param('submitted')){
my $error_message = "";
&validate_form;
if (my $error_message eq "") {
&print_form_values;
}
else {
print "<ul>\n" . my $error_message . "</ul>\n";
&print_form;
}
}
else {
&set_initial_values;
&print_form;
}
&print_page_end;
sub set_initial_values {
my $abb = "";
}
sub validate_form {
my $abb = $query->param('fn1');
# if (my $abb eq "") {
if (fn1.value eq ""){
my $error_message .= "<li>Enter abbreviation</li>\n";
}
}
sub set_form_values {
my $abb = $query->param('fn1');
}
sub print_page_start {
print $query->header;
print "<html>\n<head>\n";
print "<title>query results</title>\n";
print "</head>\n<body>\n";
print "";
print "<SCRIPT language=\"javascript\">";
print " function validfn(fnm)";
print "{";
print "fnlen=fnm.length ";
print "if (fnlen == 0)";
print "{alert(\"First name is required\")";
print " document.dataentry.fn1.focus()";
print " return false";
print "} else return true";
print "}";
print "</SCRIPT>";
print "";
}
sub print_page_end {
print "</body>\n</html>\n";
}
sub print_form_values {
print "<b>Abbrev:</b> " . $query->param('fn1') . "<br />\n";
print "<br />\n";
}
sub print_form {
print "<FORM NAME=\"dataentry\" onsubmit=\"return validfn(fn1.value)\">\n";
print "<input type\"hidden\" name=\"submitted\" value=\"yes\" />\n";
print "<b>Enter Program Abbreviation:</b>";
print "<p>";
print "<INPUT TYPE=\"text\" NAME=\"fn1\" onBlur=\"validfn(fn1.value)\"><br />\n";
print "<p>";
print "<input type=\"submit\" onclick=\"return validfn(fn1.value)\" />\n";
print "</form>\n";
}
I am including below a cgi/perl script. I am trying to validate fields in a form.
I am trying to do it in 2 ways. One is to have the script print out a javascript function in my HTML page, and the other is to do it with a Cgi function.
Neither of the two work and I cannot see why. Would someone please be able to go over my script and tell me if they see anything wrong that I am doing.
Many thanks.
m. karmali
#!/usr/local/bin/perl -w
use CGI;
my $query = new CGI;
&print_page_start;
if ($query->param('submitted')){
my $error_message = "";
&validate_form;
if (my $error_message eq "") {
&print_form_values;
}
else {
print "<ul>\n" . my $error_message . "</ul>\n";
&print_form;
}
}
else {
&set_initial_values;
&print_form;
}
&print_page_end;
sub set_initial_values {
my $abb = "";
}
sub validate_form {
my $abb = $query->param('fn1');
# if (my $abb eq "") {
if (fn1.value eq ""){
my $error_message .= "<li>Enter abbreviation</li>\n";
}
}
sub set_form_values {
my $abb = $query->param('fn1');
}
sub print_page_start {
print $query->header;
print "<html>\n<head>\n";
print "<title>query results</title>\n";
print "</head>\n<body>\n";
print "";
print "<SCRIPT language=\"javascript\">";
print " function validfn(fnm)";
print "{";
print "fnlen=fnm.length ";
print "if (fnlen == 0)";
print "{alert(\"First name is required\")";
print " document.dataentry.fn1.focus()";
print " return false";
print "} else return true";
print "}";
print "</SCRIPT>";
print "";
}
sub print_page_end {
print "</body>\n</html>\n";
}
sub print_form_values {
print "<b>Abbrev:</b> " . $query->param('fn1') . "<br />\n";
print "<br />\n";
}
sub print_form {
print "<FORM NAME=\"dataentry\" onsubmit=\"return validfn(fn1.value)\">\n";
print "<input type\"hidden\" name=\"submitted\" value=\"yes\" />\n";
print "<b>Enter Program Abbreviation:</b>";
print "<p>";
print "<INPUT TYPE=\"text\" NAME=\"fn1\" onBlur=\"validfn(fn1.value)\"><br />\n";
print "<p>";
print "<input type=\"submit\" onclick=\"return validfn(fn1.value)\" />\n";
print "</form>\n";
}