http://www.perlmonks.org?node_id=60233

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

Is it possible to use a single script to generate a page consisting of a form and a text field; submission of the form is processed by same script(which involve interaction with server) and then results is displayed on the text field on the same page dynamically? The results will accumulate on the same text box after each submission.

Normally this will be done using two scripts, one for the form and one to process the request and generate results.

Can anyone tell me if what I have in mind is possible and how? Thanks!

  • Comment on Single CGI script for submission,processing and displaying of results

Replies are listed 'Best First'.
Re: Single CGI script for submission,processing and displaying of results
by nivlac (Scribe) on Feb 22, 2001 at 20:39 UTC
    Off the top of my head, without looking at how we do it, there is a simple answer.

    On the form place a hidden variable, and check for its value with the script. If it's set to value X to action Y. This way a single script can perform multiple functions.

    While I'm not saying this is the most efficient code, it does work. (I did bother to look it up)

    $CGI = new CGI; my($item) = $CGI->param('item'); my($add) = $CGI->param('add'); my($sql, $sth, $comments) = ("", "", ""); if(defined($add)) {
    Process the form
    } else {
    Show the form
Re: Single CGI script for submission,processing and displaying of results
by tame1 (Pilgrim) on Feb 22, 2001 at 21:23 UTC
    Hopefully this is what you mean.

    use CGI; my $query = new CGI; my $name = $query->param("name"); if (!$name) { # test for a form field filled in &show_form; # if no, show form. } elsif { &process_data; # otherwise, process form data &show_result; } exit(0);
    Does that help?

    What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"
Re: Single CGI script for submission,processing and displaying of results
by SilverB1rd (Scribe) on Feb 22, 2001 at 21:10 UTC
    Check out the CGI module it shows how to do something similar to what you want. And try this page for more information on CGI.pm.
Re: Single CGI script for submission,processing and displaying of results
by sierrathedog04 (Hermit) on Feb 22, 2001 at 22:00 UTC
    If you wish to use only standard HTML then the answer is no, and here is why.

    A web browser downloads a page of HTML and parses it, usually concurrently.

    There is no mechanism in HTML for a server to change a single line of HTML once a browser has already downloaded it. Similarly, there is no mechanism for a browser to change a single line of HTML in a page that it has already downloaded.

    The only way for a server to change the value of a single form field is to download the entire page again. The questioner asks for the results to accumulate in the same text box. That does not happen with straight HTML. Rather, the server script sends a brand new page over with a brand new text box in it that has a brand new value.

    A browser can change form values locally using Javascript, a page can even include a Java applet that updates itself to display changing information. But a server cannot return a new form value to an HTML form without sending the entire page with form all over again.

Re: Single CGI script for submission,processing and displaying of results
by mr.nick (Chaplain) on Feb 23, 2001 at 01:02 UTC
    Hm. I'm not quite sure exactly what you are asking. Are you asking how to use a single script, that when invoked the first time displays the form, but on subsequent invocations adds the data from the previous submission to the current one? (whew!)

    For example, consider a form with a single input box. When the user enters something and submits it, the form is returned with the data intact, allowing him to add another chunk of text to it? Something like this?

    use strict; use CGI; my $cgi=new CGI; sub doform { my $text=$cgi->param("text"); print $cgi->header; print "<html><body>"; print "Accumilating data: $text<P>"; print "<form method=post>"; print "<input type=text name=newtext>"; print "<input type=hidden name=text value=\"\Q$text\E\""; print "</form>"; print "</body></html>\n"; } $cgi->param("text",$cgi->param("text").$cgi->param("newtext")) if defi +ned $cgi->param("newtext"); doform;