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

Please advise how I can put a perl subroutine in my cgi form. In this case I am checking for a blank entry and want to do it on my onSubmit.
<form action="actionform.pl" method=post onSubmit="return perlSub;"> <input name="value"> </form> sub perlSub { if($value eq "") { print "No value entered, please enter a value.\n" } }

Replies are listed 'Best First'.
Re: CGI form validation on submit
by benn (Vicar) on Aug 13, 2003 at 13:26 UTC

    I think you need to be a little clearer on the whole client-server communication thing. Basically, the deal is something like this.

    • Your browser requests something (say, a CGI script) from a server.
    • The server runs the requested script, and sends your browser the output - usually, a bunch of HTML / images etc. - stuff your browser knows how to render.
    • Included in that HTML may be Javascript (or JScript or similar) - something too that most browsers know how to cope with. Note that this script is generally not allowed to interact with any part of the client machine *other* than the browser.
    • If the HTML contains a form, the values entered are sent back to the server as a new request when the form is submitted. The server can examine this request and take appropriate action
    Hopefully you can see from this model that your CGI script runs *on the server*, not the client. The client simply receives the output, renders it, optionally uses client-side-scipting to process it (document.write, onSubmit etc.) and sends it back.

    As an aside, you should never *rely* on client-side scripting to validate a form. Having it as an extra check before submission is fine, but the server script should do this also to cater for those who don't run JS / hand craft forms / hack your HTML. Many many JS shopping cart systems have learnt this to their cost :)

    Hope this helps, Ben.

Re: CGI form validation on submit
by jeffa (Bishop) on Aug 13, 2003 at 13:43 UTC
    Me? I wouldn't put a Perl sub in a CGI form ... i would put a CGI form in a Perl script:
    #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); # print form print header, start_html('test for blank vals'), start_form(-method=>'POST'), 'Enter something (or not) ', textfield('value'), submit, end_form, hr, ; # get val from query - empty string as default my $value = param('value'); # remove leading and trailing space $value =~ s/^\s+//; $value =~ s/\s+$//; # have to be careful ... the empty string is defined # and zero is false, so let's check the length instead if (length($value)) { print p("You entered '$value' - thanks. :)"); } else { print p('No value entered, please try again'); }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: CGI form validation on submit
by gjb (Vicar) on Aug 13, 2003 at 12:48 UTC

    Although you can use Perl as a scripting language in a browser under Windows if you've installed ActiveState's Perl, I don't think it's a good idea to go that way. IMHO it's best to stick to JavaScript for such things.

    Just my 2 cents, -gjb-

      Actually my cgi form populates a document. I am currently using JavaScript to do my blank validation checks but now want to use perl to do blank validation checks and also check the document to see if the data entered already exists. So I am assuming I need to do this with something that can open up a file and check if the data exists before I populate the document. I also assume I can do this with the onsubmit?

        It seems to me you're confused about client vs. server functionality. If you want to store the data entered in your form after it's been submitted, you're supposed to do this serverside, i.e. on the webserver you're submitting the form to.

        It depends on the webserver you use what's the best way go, but I suppose you'll want to use CGI on the server (which can, but need not, be a perl script).

        If you've no experience with CGI programming (or even don't know what I'm ranting about), have a look at the tutorial section of the Monastry, there you can find quite a number of introductory texts on various aspects (the ones by Ovid seem relevant for you).

        Hope this helps, -gjb-