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

My Perl cgi script "ip2loc.pl" is called once from an x.shtml with an
<!--#exec ...-->.
Unfortunately it executes twice:

Ip2loc.pl creates an HTML form with cgi.pm, and "prints" it to the browser. When ip2loc.pl is initially executed, it creates the form properly but does not wait for the Submit to be pushed before going ahead and processing the (null) output from the form. Eventually, when the form is filled out and the Submit button IS pushed, ip2loc.pl executes in its entirety (from its beginning) a second time. This second time the data is present and duly processed by ip2loc.pl.

How do I get ip2loc.pl to pause after creating the form until the Submit button is pushed?

[Possibly involved: my script is initiated from the middle of an HTML page (which of course already has a header) which forms a boilerplate style frame for my script's Form. My script issues a header too (implicit action of the start_html() call - see attachment below). Perhaps a single HTML page cannot have two headers. If this is the case, my question becomes: how would I prevent CGI.pm from issuing a header for the Form in question?]


Thank you,
Peter Schwenn
p.s. The code in my script ip2loc.pl which creates the HTML Form is:
... use CGI ':standard'; .... print start_html('Get Email Address'), h3('Input the email address to receive the Download Link:'), startform("GET"), "Email Address : ", textfield("clientEmail",""), " ", submit("Submit"),"<br/><br/ >", endform(), ; ...

20080718 Janitored by Corion: Fixed square brackets, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Cgi script executing twice
by pc88mxer (Vicar) on Jul 17, 2008 at 15:38 UTC
    How do I get ip2loc.pl to pause after creating the form until the Submit button is pushed?
    Your script will not explicitly pause. The logic of your application is (or should be):
    if there are submitted form variables then perform the requested action else display the form end if
    The first time your page is called there are no form variables submitted so your script will just display the input form. When the user hits the Submit button your script is called again. This time, however, there are form variables present in the request so your script will perform the action based on the submitted values.

    A way to check if form variables are present is to simply check param():

    use CGI qw/:standard/; ... if (param()) { # form variables present } else { # display the form }
Re: Cgi script executing twice
by bradcathey (Prior) on Jul 17, 2008 at 15:46 UTC

    schwenn, check out this basic tutorial on CGI.

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Cgi script executing twice
by pileofrogs (Priest) on Jul 17, 2008 at 16:50 UTC

    It's important to keep in mind that your script is doing two jobs. 1) showing the form, 2) processing the form. You could write two cgi, one that makes the form and one that processes it. Or you could write a static HTML form that submits to the cgi that processes the form. You're doing it the most popular way, but not necessarily the most intuitive way.

    Oh, and regarding your 2nd header. It will show up as text in the HTML produced by the CGI. Remember all HTTP is text. Try opening a telnet session to a web server. You don't need to call start_html() if you already have a header and an opening <html> tag.