I'm not quite clear on what the code is trying to accomplish, but I suspect what happens is that the variable list is never cleared. When you come back for the third run through the script, you still populate all the $FORM{$name} values that were present on the second page, so hitting refresh just means the third page will have the values you set on the second page, rather than no values at all.

I think you will have less trouble with the logic if you set up two HTML output pages, one for the initial (empty) condition, and the other for the display condition. Then give each "submit" button a different name so you can test in the code which page the submit came from, and then do the right thing.

In the first (initial) condition the submit button might say:

 <input type="submit" name="initial_page" value="See the Results!">

while the second (display) page button says something like "Try again!", as in:

<input type="submit" name="display_page" value="Try again!">

So following your current coding, you would show the second (value) page only if $FORM{initial_page} has a value; otherwise you would show the first page.


A couple of coding hints. You can simplify presenting HTML documents by using a HERE document; this lets you display HTML unchanged between the starting and ending tag. The 3rd edition of the Camel book Programming Perl has a good description of these on pages 66-67. For example, you could change your code printing out the start form statement to something like:

if ($line =~ /<!--InputWords-->/i) { print <<EOF; <FORM METHOD=POST ACTION=$CGIURL> <INPUT TYPE=HIDDEN NAME=TextFile VALUE="$TextFile"> <P><CENTER> <TABLE> EOF foreach $variable (@sortedvariables) { next if ($variable eq $lastvariable); print <<EOF; <TR><TD ALIGN="RIGHT"><P>$variable: </TD> <TD><INPUT TYPE="TEXT" NAME="$variable" SIZE="25"> </TD></TR> EOF $lastvariable = $variable; } print <<EOF; </TABLE></P>; <P><INPUT TYPE="SUBMIT" VALUE="See the Results!"> </CENTER></P></FORM> EOF } else { print $line; }

All the characters between print <<EOF; and EOF (on a line by itself, flush left, no spaces after, just a \n) will be printed unchanged, including the carriage returns. All Perl variables in the block (like $variable) will be replaced by its current value. Makes life much easier.

Also you really should learn about CGI.pm. Your first lines of code, where you retrieve form values, could be as simple as:

use strict; use CGI; $q=CGI->new(); if ($q->param('initial_page') { [ do the stuff to display the results page ] }

The mere issuing of the command "new CGI" gathers in all of the form field names and their values from the last invocation of the script, does all the necessary decoding, removing +'s and other funny marks, and makes the full list available to you, so that "$variable1 = $q->param('variable1') will return the value of the parameter variable1 (assuming it is a single value and not a multivalue field). And all this without extra work by you! So good modules like this one will save lots of work, prevent lots of errors, and let you concentrate on the new stuff in your code.

Hope this helps.

Live in the moment

In reply to Re: Refreshing a perl script by Speedy
in thread Refreshing a perl script by wolverina

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.