in reply to Re^3: Web form to alter files is writing to file before submit button is pressed.
in thread Web form to alter files is writing to file before submit button is pressed.

I don't see how that has anything to do with this case -- except that the OP is missing a redirect.

As it is now, if the user reloads the page, the form will get re-submit (and the file modified) without a single warning. If the method is a POST, the browser will at least warn the user that he may not want to do the reload.

  • Comment on Re^4: Web form to alter files is writing to file before submit button is pressed.

Replies are listed 'Best First'.
Re^5: Web form to alter files is writing to file before submit button is pressed.
by Anonymous Monk on Mar 12, 2012 at 07:45 UTC

    I don't see how that has anything to do with this case

    You suggested switching to a GET request for something that CHANGES the resource being accessed -- this goes specifically against every recommendation and accepted common wisdom

    As it is now, if the user reloads the page, the form will get re-submit (and the file modified) without a single warning. If the method is a POST, the browser will at least warn the user that he may not want to do the reload.

    You specifically suggested making the request a GET request

      Let me tell you two (?) anonymous monks what I want to ultimately accomplish with this script which may help you point me in the right direction. I should have done that initially, but as usual, I was a little shortsighted. I have made some small changes to the script since the original post, so I will post the updated script at the end of this post.

      1. Load the form into the browser with the data in text fields for editing.
      2. After editing the data, save the data to the same file it came from.
      3. Reload the form with the updated data for possible further editing.

      So far, with the few changes made, I am able to accomplish the first and second items on the list. The changes are that I changed the method from get to post and added a conditional to the code at the end to check if there were any changes made. I have not gotten to the third item yet, but I am still thinking of ways to accomplish it.

      For the future, I am going to make a subroutine or subroutines out of this so that I can use it for all of my data editing needs.

      So, if you see anything else I need to change, your help is always appreciated.

      #!/usr/bin/perl use strict; use warnings; use CGI; use File::Basename; use HTML::Entities qw(encode_entities); use lib 'lib'; use Base::HTML qw(start_html end_html); use Base::Roots qw(get_data); use Base::Data qw(get_hash); use Base::Nifty qw(line); my $form = basename($0); my $file = get_data('Base','other_sites.txt'); my %sites = get_hash( file => $file, headings => [qw(name link)], ); my $cgi = CGI->new(); start_html; line(3,qq(<form action="$form" method="post">)); line(4,q(<fieldset>)); line(3,q(<table>)); for my $site (sort {lc $a cmp lc $b} keys %sites) { my $name = $sites{$site}{name}; my $link = $sites{$site}{link}; $sites{$site}{new_name} = $cgi->param("$site\_name"); $sites{$site}{new_link} = $cgi->param("$site\_link"); line(4,q(<tr>)); line(5,qq(<td><input type="text" id="$site\_name" name="$site\_name" + value="$name"></td>)); line(5,qq(<td><input type="text" id="$site\_link" name="$site\_link" + value="$link"></td>)); line(4,q(</tr>)); } line(3,q(</table>)); line(5,q(<input type="submit" value="Make changes">)); line(5,q(<input type="button" onclick="location='$form'" value="Start +over">)); line(4,q(</fieldset>)); line(3,q(</form>)); end_html; if ($cgi->param) { my @new_lines; for my $site (sort {lc $a cmp lc $b} keys %sites) { my $new_name = defined($sites{$site}{new_name}) ? $sites{$site}{ne +w_name} : $sites{$site}{name}; my $new_link = defined($sites{$site}{new_link}) ? $sites{$site}{ne +w_link} : $sites{$site}{link}; push @new_lines, "$new_name|$new_link"; } open(my $fh,'>',$file) or die "Can't open $file"; print $fh join("\n",@new_lines); }
      Have a cookie and a very nice day!
      Lady Aleena