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

As the title says I'm a CGI newby. I have noticed that on my system (apache on a unix workstation) my CGI scripts don't send anything to the browser until they are finished. I'd like to change this so that I can send the user a form during runtime to ask for necesary extra input before the script continues. Is there a way of doing this?
  • Comment on A CGI newby question:sending data to a browser during runtime

Replies are listed 'Best First'.
Re: A CGI newby question:sending data to a browser during runtime
by dda (Friar) on Jul 18, 2002 at 10:14 UTC
    No way. You should split your script into parts or make it to act in steps, passing intermediate values in hidden fields. Check out this node for additional info.

    --dda

Re: A CGI newby question:sending data to a browser during runtime
by tadman (Prior) on Jul 18, 2002 at 11:50 UTC
    Unless you can wave a magic wand and change the way HTTP works, no. You have to just make do. Here's an idea of what you might do, though:
    use CGI; my $q = CGI->new(); print $q->header(); if ($q->param('question')) { print finished_form(); } else { print ask_question_form(); }
    You could test for more than one parameter, plus check if they are valid. The two functions, of course, display HTML, presumably populated with various bits of data, or HTML form elements.

    The output of ask_question_form is an HTML page with the ACTION of the form set to be the same page. Which is to say that it's unset. Your script displays the HTML form, and displays the processed result. This can be extended to make multi-part forms quite simply, where the results are carried forward from one form to the next.

    This is, of course, an extremely trivial example, but most handlers, while much more complicated, are fundamentally the same in concept.
Re: A CGI newby question:sending data to a browser during runtime
by Anonymous Monk on Jul 18, 2002 at 12:55 UTC
    Thanks! I had this gut feeling that it wasn't going to be possible. I am currently using something similar to that offered in the second response with lots of hidden tags to pass the data. The problem is I'm working with some larger arrays which I sort of don't like passing that way. I shall soldier on...