boat73 has asked for the wisdom of the Perl Monks concerning the following question:

Hello again wise ones. I am hoping someone can help me do the following. using the html and cgi code below this is what I would like to do. Have the cgi test the form as it does and if it finds a problem return the user back to the html form but fill the fields with whatever data they already entered and display an error message at the top. I can of course print the error and tell them to click back but would rather save them the clicks. I tried a redirect and passing the args using $key=$value but that didn't work. I know this is prob something simple but I am stumped. Thanks in advance for any help.
<html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=windows-12 +52"> <title>Name</title> </head> <body> <form name=form method="POST" action="http://localhost/vfsscripts/junk +.cgi"> <blockquote> <blockquote> <blockquote> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb +sp;&nbsp;&nbsp; Name&nbsp; <input type="text" name="Name" size="20"></ +p> <blockquote> <blockquote> <p>Email&nbsp;&nbsp;&nbsp; <input type="text" name="Email" size="20"></p> <p><font face="Verdana" size="1"> <input type="submit" value="Submit" tabindex="37"></font></p> <p>&nbsp;</p> </blockquote> </blockquote> </blockquote> </blockquote> </blockquote> </form> </body> </html>
#!c:/Perl/bin/Perl.exe use CGI; print "Content-type: text/html\n\n"; $query = new CGI; &testform(); sub testform{ $Name = $query->param("Name"); $Email = $query->param("Email"); if(($Name !~ /\w/) || ($Email !~ /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0 +-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)){ #### SEND BACK TO HTML FOR AND FILL IN WITH DATA ALREADY ENTER +ED #### } else { print "Name => $Name<br>"; print "Email => $Email<br>"; } }

Replies are listed 'Best First'.
Re: have cgi send data back to form on error
by chb (Deacon) on Feb 11, 2005 at 07:15 UTC
      If you have use CGI.pm to generate the form and then check it with the same script, you can set the form's submit action to self_url. If there are problems with the user input, you can then just print the form again and the previous input will be in there automatically.

      :)
Re: have cgi send data back to form on error
by Errto (Vicar) on Feb 11, 2005 at 04:14 UTC

    How is the HTML form being generated initially? You don't show us what you're redirecting it back to. If that page is generated with CGI.pm's HTML generation methods, then the form inputs will be automatically pre-filled with the values you pass to that script, assuming the names match. Otherwise, if for example it's a static HTML page, you will have to do it yourself. Take the HTML you have and turn it into a template, using perhaps HTML::Template or the Template Toolkit. Each place you have an input tag or textarea, put in the appropriate template tag to fill in the value. Then from your CGI script, invoke the template passing in the form values. The docs for the modules I mentioned give plenty of examples.

      The HTML is being generated with frontpage. I use frontpage because I admit the gui (ouch) is easy to use and quick to make changes. I feel so ashamed. I have not realy generated HTML with CGI.pm just using print <<ENDHTML but maybe I will have to look into it. The template idea has potential also but still a little more work to modify than with frontpage. Thanks for the direction.
        print <<ENDHTML can work here as well. Since Perl interpolates strings, just stick the variables you want in the appropriate places like:
        use HTML::Entities; my $email = $q->param('email'); $email = encode_entities($email); print <<ENDHTML; <input type="text" name="email" value="$email"> ENDHTML
Re: have cgi send data back to form on error
by TedPride (Priest) on Feb 11, 2005 at 06:35 UTC
    What you need is some sort of routine that loads your form page, looks for field data insertion points, and adds the data. For instance, instead of:
    <input type="text" name="Email" size="20">
    you might have:
    <input type="text" name="Email" size="20" value="Email">
    And then a routine might load the page into a variable and perform the following replace (untested, but should theoretically work):
    $text =~ s/value="(.*?)"/'value="'.$query->param($1).'"'/eg;
    This will of course not work for textareas, selects, etc., but a slightly more advanced templating system will be able to manage those as well.
Re: have cgi send data back to form on error
by Popcorn Dave (Abbot) on Feb 11, 2005 at 17:37 UTC
    What you want to do is use CGI.pm to check to see if you have parameters set, and if not display your initial html code. Make sure you don't put an action in your form, though. That way the CGI.pm module will redirect the information to the calling URL.

    Pseudo code:

    if (param()){ do your error checking here exit; # exit the cgi script if all is correct } HTML code here ... ...

    Bascially what this does, it to see if you have called the script with any parameters. If not, then it defaults to the displaying of the HTML code. If you have passed the parameters, then you can see which ones need to be processed and/or corrected and go from there.

    I believe there are examples of this in the CGI docs, plus there are a few examples of it on the web.

    HTH!

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.