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

This question is from another question I asked, but it may be to buried for people to see, so I am resending it as a new question.

I'm still a little lost. That tutorial from Ovid didn't really help, except to clean up my html. But I still have the problem of how to getting a send button and a file write to both to work at the same time.

Here is code for what I am doing:

# Setup up # Variables # and define work fields # If file exists, append to it if (-e $datefile) { open (BOBIN, ">>$datefile") || die "Cannot Open File $datefile for + writing: $!"; # Open Data file } else { ### Else open directory where files data files are located # Sort in descending order, pick out first file # And e-mail it # Then create a new file with current data as name open (BOBIN, ">$datefile") || die "Cannot create File $datefile f +or writing: $!"; } # The param function to see if a button was clicked # If not, go on if (!(param())) { # Do stuff # By processing data # put up web page # with formatted data <form> <center><input type=submit value=Send name=S1> </center></form> } else { # When it is print # Redirect to another web page if (param('S1') eq "Send") { print BOBIN $datajoin; use CGI; print redirect("http://www.gailborden.info/services/b +ob/submitq.htm");} } close(BOBIN);
I keep getting an error that it cannot open the file. But I put the param up farther near top of the program it works, but it still won't write the file. My theory is: When I do a button click, I noticed, and was told, it does an http post, so I assuming it wipes out the data

Is there easier way to just redirect it to a confirmation page AND write the data after pressing a button?

Does anyone know, if I change the "input type" to button can I do the same thing? When clicked, redirect to confirmation page and then write the data.

I will be more then happy to send the full script and web form.

Joseph A. Ruffino
Automated Systems Assistant
Gail Borden Public Library District
270 N. Grove Ave
Elgin, Il, 60120
847-742-2411 x5986

Replies are listed 'Best First'.
Re: Updated Button Question
by ikegami (Patriarch) on Aug 12, 2004 at 14:29 UTC

    If I understand correctly, you want to send the parameters both to your script (so you can print them) and to a second script (submitq). That's quite tricky because HTTP & HTML designers failed to forsee the need to do a POST redirection. If you only needed to send it to submitq, then you could:

    <form ...> <center><input type=submit value=Send name=S1></center> </form>

    to:

    <form method="POST" action="http://.../submitq.cgi"> <center><input type=submit value=Send name=S1></center> </form>

    One way would be to get JavaScript to do a post by changing:

    # When it is print # Redirect to another web page if (param('S1') eq "Send") { print BOBIN $datajoin; print redirect("http://.../submitq.cgi"); }

    to:

    if (param('S1') eq "Send") { print BOBIN $datajoin; <body onload="form1.submit()"> <form name="form1" method="POST" action="http://.../submitq.cg +i"> foreach ($cgi->params()) { <input type=hidden name="$_" value="$cgi->param($_);" > } </form> }

    Another alternative is a "subquery". Change:

    # When it is print # Redirect to another web page if (param('S1') eq "Send") { print BOBIN $datajoin; print redirect("http://.../submitq.cgi"); }

    to:

    if (param('S1') eq "Send") { print BOBIN $datajoin; require LWP::UserAgent; require HTTP::Request::Common; $ua = ...; $ua->request(POST 'http://.../submitq.cgi', [ params ]); print the response. }

    As an aside, open(FH, ">>...") will create the file if necessary, so

    if (-e $datefile) { open (BOBIN, ">>$datefile") || die "Cannot Open File $datefile for + writing: $!"; # Open Data file } else { ### Else open directory where files data files are located # Sort in descending order, pick out first file # And e-mail it # Then create a new file with current data as name open (BOBIN, ">$datefile") || die "Cannot create File $datefile f +or writing: $!"; }

    simplifies to

    open (BOBIN, ">>$datefile") || die "Cannot Open File $datefile for + writing: $!"; # Open/Create Data file
      ikegami,

      Actually, all I want is to be able to write the data, and redirect to new web page say it was done.

      If you know of another way other than using param, it would be great. If not, all I need is the one field wriiten to the txt file.

      I can do it straight, by just writing it as soon as the web page is created, but I want to give them a choice wether to write it or correct it, then write it.

      Joseph A. Ruffino
      Automated Systems Assistant
      Gail Borden Public Library District
      270 N. Grove Ave
      Elgin, Il, 60120
      847-742-2411 x5986

        Actually, all I want is to be able to write the data, and redirect to new web page say it was done.

        Doesn't your code already do that?

        If you know of another way other than using param, it would be great. If not, all I need is the one field wriiten to the txt file.

        The server has to be able to tell what you want to do. One option is to use a param like you did. That's very proper.

        Another way is is by having multiple scripts. Specifically, script2.cgi would be the contents of if (param('S1') eq "Send") block, and script1.cgi would have the rest. script1.cgi would specify script2.cgi in the action field of the of the <form> element.

        It's a question of prefence, but checking the param like you are already doing is probably the most straightforward.