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

I have written simple example of post method in cgi script Here is the simple form
<FORM action="/cgi-bin/hello_post.cgi" method="POST"> First Name: <input type="text" name="first_name"> <br> Last Name: <input type="text" name="last_name"> <input type="submit" value="Submit"> </FORM>
below is the hello_post.cgi to handle input given by web browser
#!/usr/bin/perl local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } $first_name = $FORM{first_name}; $last_name = $FORM{last_name}; print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title>Hello - Second CGI Program</title>"; print "</head>"; print "<body>"; print "<h2>Hello $first_name $last_name - Second CGI Program</h2>"; print "</body>"; print "</html>"; 1;
Here you can see that my logic and html tag are in same cgi script. My question is that how can i display the out in separate html form. means i want to separate business logic in cgi script and presentation logic in .jsp or .html etc

Replies are listed 'Best First'.
Re: How to display output in separate html form in cgi script
by Kenosis (Priest) on Jan 24, 2014 at 04:29 UTC

    If you're going the Perl CGI route, consider using the CGI module (sorry, chromatic).

    If I understand your question correctly, you want a separate CGI script and a resulting html document. That is, instead of a dynamically-created html document, you'd like a static html document created by the script.

    This can certainly be done, by writing the results to an html file, and then redirecting the browser to that file. You'd need write permissions for both the dir and file you're writing to. But now it gets a little messy...

    You'd need to create a unique filename for each response--for each new htnl document that the CGI script creates. Additionally, you'd need to figure a way to deal with all the html files that your CGI script is creating. Do you leave them in the directory? If you're going to delete them, how long after they're created and how often?

    You need to answer for yourself whether creating static html files from a CGI script is better than creating the pages 'on the fly,' as your script above does.

      I mean to say that i want to redirect the result to another cgi script suppose result.cgi which will display the output
      print "<html>"; print "<head>"; print "<title>Hello - Second CGI Program</title>"; print "</head>"; print "<body>"; print "<h2>Hello $first_name $last_name - Second CGI Program</h2>"; print "</body>"; print "</html>";
      but how result.cgi will fetch the $first_name $last_name from hello_post.cgi?
        The same way your script does - using $ENV{'QUERY_STRING'};

        As suggested earlier, the CGI module (use CGI;) makes programming this a lot easier, by offering you param("first_name") etc.

                If your eyes hurt after you drink coffee, you have to take the spoon out of the cup.
                      -Norm Crosby

        I mean to say that i want to redirect the result to another cgi script suppose result.cgi which will display the output

        Um, what do you mean by redirect? HTTP redirect (aka location header)?

        Or do you simply want to change the form action to  <FORM action="something.else/the_script_i_really_wanted.cgi"  > ??

Re: How to display output in separate html form in cgi script
by Anonymous Monk on Jan 24, 2014 at 03:47 UTC

    question is that how can i display the out in separate html form. means i want to separate business logic in cgi

    The question doesn't make sense, what is "separate html form" mean?

    This is how CGI works
    1) browser makes http request ... url something something ... cgi-bin/ foo...
    2) server runs "foo" cgi program .... and the "foo" cgi program prints http headers and http content

    "http content" can be any bytes of html you want however you want it

    So what is it that you're asking how to do?

    This link learn about the internet and Re: Perl and jqwidgets (try knots) links tutorials and examples on learning how CGI works, how to use CGI module, how to debug cgi programs, and things of that nature ...