Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-06-20 23:58 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.