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

I have a perl script with a HTML form in it that prints out on to the page for user input. But the question i have is their a way to do an if statement like. if post request then do this?

Replies are listed 'Best First'.
Re: Form inside perl script
by ww (Archbishop) on May 07, 2008 at 01:58 UTC

    Since your question isn't clear to me, here are a few shots in the dark:

    Assumption 1 (and begging the question of what triggers your original script): You are looking for a way to chain .cgi scripts.

    OK. Let's say you have a script which, when executed on the server, generates a new .html page. The generated page (whether created with a heredoc (for example, the "here documents" section of quotes in Perl) or some other method (see, for example, the thread beginning at Template with optional PHP execution, which, despite the title, offers a quick survey of templating systems) looks something like this:

    <Doctype ...> <html> <head> <title><metas><etc> </head> <body> <p>Some content</p> <form method="POST" action="cgi-bin/webmstr.cgi"> <input type="hidden" name="subject" value="somevalue" readonly> <FIELDSET> <LEGEND>Personal information</LEGEND> <LABEL for="firstname">First name: </LABEL> <input id="firstname" name="firstname" type="text" size="30"> <br> <LABEL for="comments">Please enter your submission here: </LABE +L> <br> <TEXTAREA id="comments" NAME="comments" ROWS="10" COLS="72"></TEXT +AREA> <!-- Button(s): --> <input type="submit" value="Send this form?">&nbsp; &nbsp; <input +type="reset"> <br> </FIELDSET> </form> ... </body> </html>

    Note Line 8 which -- as one way I interpret your question -- is an answer to "is their (sic) a way to do an if statement like. if post request then do this?." When the user submits the form by clicking the submit button in the generated page, the browser uses the POST method to ask the server to execute webmstr.cgi. Your "if" is implicit: If the user doesn't click the submit button, nothing more happens. If the user does click submit, then whatever instructions you've placed in webmstr.cgi will be executed.

    Another answer might be "Of course, but it's up to your script to identify what's being submitted in what circumstances and what do YOU want your script to do about it." However, a full tutorial on conditionals, cgi, etc, is beyond the ambit of this response.

    Assumption 2: You knew all that and are wondering about passing data back to the same script for action on the response.

    OK, that's also simple enough. Parse the parameters returned by the second submit for a relevant value (use a form id, for example), and, using an if (...) { clause;}, do something with the rest of the parameters.

    Assumption 3: I'm entirely blind to your real question and intent.

    That's certainly a possibility, in which case I can do no better than guess you may need to read the CGI.pm docs and available_tutstring (except that the linked there may be down [discussion: http://use.perl.org/~Ovid/journal/32708]).

    Oh, come to think of it, yes I do have a better suggestion: read How do I post a question effectively? and "show us some code."

Re: Form inside perl script
by kyle (Abbot) on May 07, 2008 at 02:14 UTC

    It sounds like you're asking how to tell if you got a POST request or a GET request. CGI can tell you this with request_method() like so:

    use CGI; my $cgi = CGI->new(); if ( 'POST' eq $cgi->request_method() ) { # other methods: GET, HEAD }
Re: Form inside perl script
by Gangabass (Vicar) on May 07, 2008 at 01:54 UTC