Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^6: Web form to alter files is writing to file before submit button is pressed.

by Lady_Aleena (Curate)
on Mar 12, 2012 at 17:46 UTC ( #959150=note: print w/replies, xml ) Need Help??

in reply to Re^5: 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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://959150]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-01-22 18:32 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (235 votes). Check out past polls.