Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Web form to alter files is writing to file before submit button is pressed.

by Lady_Aleena (Deacon)
on Mar 10, 2012 at 16:58 UTC ( #958880=perlquestion: print w/ replies, xml ) Need Help??
Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:

I created a web form to help me alter my data files. However, the section of the script (lines 44-51) is altering the file on the first load of the script into my browser instead of waiting for me to press the Make changes (submit) button. Is there any way to tell it not to alter the file on the first load of the script? Any other suggestions to make this work better?

#!/usr/bin/perl use strict; use warnings; use CGI; 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 $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,q(<form action="form_test.pl" method="get">)); 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(4,q(</fieldset>)); line(3,q(</form>)); end_html; my @new_lines; for my $site (sort {lc $a cmp lc $b} keys %sites) { my $new_name = defined($sites{$site}{new_name}) ? $sites{$site}{new_ +name} : $sites{$site}{name}; my $new_link = defined($sites{$site}{new_link}) ? $sites{$site}{new_ +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

Comment on Web form to alter files is writing to file before submit button is pressed.
Download Code
Re: Web form to alter files is writing to file before submit button is pressed.
by choroba (Abbot) on Mar 10, 2012 at 17:04 UTC
    Run the open and print only if the submit button was pressed.

      choroba, I do not know how to assign a value to the pressing of the button.

      Have a cookie and a very nice day!
      Lady Aleena
        It's done automatically, just inspect params (I haven't done CGI for several months so I am not sure about the details, you might need to add name to your button or something).
Re: Web form to alter files is writing to file before submit button is pressed.
by tangent (Curate) on Mar 10, 2012 at 17:46 UTC
    If you change the line
    line(5,q(<input type="submit" value="Make changes">));
    to:
    line(5,q(<input type="submit" name="make_changes" value="Make changes" +>));
    then you can check if the button was clicked:
    if ( $cgi->param("make_changes") ) ...

      I made this change, and it made a big difference. Thank you tangent for the help.

      Have a cookie and a very nice day!
      Lady Aleena
Re: Web form to alter files is writing to file before submit button is pressed.
by aaron_baugher (Deacon) on Mar 10, 2012 at 17:56 UTC

    A common way is to use the param() function of CGI.pm to see whether any parameters were sent. That way you don't need a named submit button at all, which you may not always have. (Or you may have more than one.)

    if( $cgi->param ){ # do stuff with submitted form data } else { # print form }

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      aaron, so this checks all of the parameters I set, and if any of them are defined, then it will do stuff with the submitted form data. There may be one small problem with having to check all of those parameters which is some of my files will have tens of thousands of parameters to check. I am not overly worried about speed, but would that slow things down a little?

      Have a cookie and a very nice day!
      Lady Aleena

        That's not quite what it does. $cgi->param returns a list of the names of all the form fields submitted by the browser. So if any are submitted, it will return true, regardless of their values. Only when the URI is requested without any POST data -- as when the page is first loaded, typically -- will it return nothing, giving a false status to the if statement. So it's not really "checking all the parameters." To the extent that it loads them into some sort of structure, I'm fairly sure that was already done when you created the CGI object, so there's not much cost here.

        On your last question: If you have forms with that many fields, I think you'll run into bottlenecks at the browser or the web server -- with the maximum POST data size, for instance -- long before counting the items in a large array slows down your Perl script. With a form that large, I'd be more concerned about usability and maintainability than with speed.

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Re: Web form to alter files is writing to file before submit button is pressed.
by Anonymous Monk on Mar 11, 2012 at 05:08 UTC
Re: Web form to alter files is writing to file before submit button is pressed.
by TJPride (Pilgrim) on Mar 11, 2012 at 06:37 UTC
    Naming the submit button isn't enough, since people can submit forms by hitting Enter on a text field, in which case the named submit button isn't referenced. What I do is include a hidden field called submitted and then check for that:

    <input type="hidden" name="submitted" value="1">

    if (param('submitted')) { ### Do something }

      since people can submit forms by hitting Enter on a text field, in which case the named submit button isn't referenced.

      Just FWIW, last I heard, only IE does that. The other browsers will send a submit argument.

      As for what to do to avoid that, I either check that the request is POST, or check for a required param's existence. If the request does something that is not read-only (like the OP's script seems to do), I'd very strongly consider changing the form submission to POST from GET.

        If the request does something that is not read-only (like the OP's script seems to do), I'd very strongly consider changing the form submission to POST from GET.

        That is against the common accepted wisdom of Post/Redirect/Get

      the OP wants to know if the submit button has been pressed, as stated in the title, not if the enter key has been hit.

        Don't worry, I'm glad that using the enter key has been brought up in this discussion since not everyone uses just the buttons presented. I forgot to take that into consideration, and I use the enter key to submit data a lot of the time. I should have thought of it when posting the original post of this discussion.

        Have a cookie and a very nice day!
        Lady Aleena

      Thank you for mentioning hitting enter. I should have considered that in the original post.

      Have a cookie and a very nice day!
      Lady Aleena
        I often use CGI::Minimal to see what is going on with form inputs. It has a handy function called "calling_parms_table()" which returns a HTML table of all form and environment variables. It will show you what happens when you hit Enter instead of pressing the button - the result seems to vary across different browsers. This becomes important if, say, you had two buttons on the form: make_changes and undo_changes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://958880]
Approved by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-12-18 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls