Here's another way. Add a comment - <--error_message--> - to the form page where you want the error message to display, then use something like this:

#!/usr/bin/perl -w use strict; use CGI; # create CGI object my $q = new CGI; # create list of fields to check my @non_empty_form_fields = qw(name email address tel); # store errors here my @err = (); # check fields exist for (@non_empty_form_fields) { unless ( $q->param($_) ) { push @err, "You must complete the '$_' field"; } } # if error, represent if (@err) { # read in form open (FORM,'/path/to/form.html') || die $!; my $form = join '', (<FORM>); close(FORM); # create error message my $err_message = $q->h3('Error'). $q->p('I could not process the form:'). $q->ul( $q->li([@err]) ). $q->p('Check your input and resubmit.'); # put message in page $form =~ s/<!--error_message-->/$err_message/is; # show page print $q->header, $form; exit(0); } # otherwise input ok, so do whatever...

If you are using relative URLs to call page images, you may also want to add a <BASE HREF...> tag to the page (or get the script to add it).

HTH

cLive ;-)


In reply to Re: Empty form fields error message by cLive ;-)
in thread Empty form fields error message by n4mation

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.