Professional Web Applications Themes

Newbie: form checkboxes & param - PERL Miscellaneous

I have a script to print out the values of a form's fields to a file. That works fine. However I wanted to print out the values of all the checkboxes, not just the ones checked, to make ysis easier. I shortened my script to deal with just the checkboxes until I got this matter settled. I found several ideas on the web about using param with checkboxes. I tried two but, being a newbie, I do not know how to fix the script. Debug says I have compilation errors near line 24 and "0". I tried a couple things ...

  1. #1

    Default Newbie: form checkboxes & param

    I have a script to print out the values of a form's fields to a file.
    That works fine. However I wanted to print out the values of all the
    checkboxes, not just the ones checked, to make ysis easier. I
    shortened my script to deal with just the checkboxes until I got this
    matter settled. I found several ideas on the web about using param
    with checkboxes. I tried two but, being a newbie, I do not know how to
    fix the script. Debug says I have compilation errors near line 24 and
    "0". I tried a couple things but got nowhere.

    My checkboxes are called "c1", "c2", "c3", "c4".

    #!/usr/bin/perl -w
    use CGI qw(:standard);

    print "Content-type:text/html\n\n";

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    pairs = split(/&/, $buffer);
    foreach $pair (pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
    }

    print "<html><head><title>Results</title></head>\n";
    print "<body>\n";
    print "<h2>Results</h2>\n";

    boxes = ("c1", "c2", "c3", "c4");
    my $key;
    my $box;
    foreach $key(boxes) {
    $box=param($FORM{$key})||'0'; # print value or zero
    print "$box|\n";
    }

    print "</body></html>\n";

    Thanks in advance for your help.

    Nadine Guest

  2. #2

    Default Re: Newbie: form checkboxes & param


    "Nadine" <sdsu.edu> wrote in message
    news:bksk5j$to3$sdsu.edu... 
    to 

    Untested:

    #!/usr/bin/perl -l
    use CGI qw ':standard';
    use strict;
    use warnings;

    print header, start_html;

    print title ('Results');
    print '<body>';
    print h2 ('Results');

    print "$_ => {[ param ($_) ]}" for grep /^c/, param();

    print '</body></html>';


    if you name all your checkboxes starting with a 'c' ... you will get the
    name, and values of only those parameters...




    gibbering Guest

  3. #3

    Default Re: Newbie: form checkboxes & param

    On Wed, 24 Sep 2003 10:33:02 -0700, Nadine wrote: 

    Good! You're using the CGI module. You should, however, use the strict
    module as well;

    #!/usr/bin/perl
    #
    use strict;
    use warnings;
     

    You could also let the CGI module do this work for you:

    print $cgi->header(-type => 'text/html');
     

    Aaaargh! Earlier on you declared that you wanted to use the CGI module,
    but you're not! Do _not_ p STDIN yourself. Instead use the famous
    CGI module. Example:

    my $cgi = CGI->new();
    my $value = $cgi->param( 'keyword' ) || '';
     

    Use the CGI.pm module and rewrite this as something like this:

    foreach my $box ( qw(c1 c2 c3 c4) ) {
    my $value = $cgi->param( $box ) || 0;
    print $value . "|\n";
    }


    --
    Tore Aursand <no>
    Tore Guest

  4. #4

    Default Re: Newbie: form checkboxes & param

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Nadine <sdsu.edu> wrote in news:bksk5j$to3$sdsu.edu:
     


     

    Gah!! This is the third or fourth time in the past two weeks that same
    code for parsing CGI parameters! IT IS VERY VERY BAD CODE!! Where are
    you people *getting* this crap??

    You are already using the excellent CGI module. It does the CGI
    parameter parsing for you, and it does not suffer from the many bugs that
    the parsing code you're actually using has:

    1. It fails if CGI parameters are separated by semicolons.
    2. It fails if a CGI value has an unencoded equals sign in it.
    3. It fails if a CGI parameter has multiple values (eg checkboxes).
    4. It will not work for GET method, only POST method.
    5. It does not limit the quantity of data uploaded, leaving you open
    to possible DOS attacks.
    6. It does not unencode escaped characters in the parameter name.

    Seven lines of code, six bugs.

    Delete these seven lines, and start writing "param($name)" instead of
    "$FORM{$name}". Also, start writing

    print header();
    instead of
    print "Content-type: yadda yadda yadda\n\n";

    One, it's less typing; two, you're less likely to make a typo that will
    give you confusing 500 errors.

    Now, on to your actual question:

     

    I don't see any syntax error on this line. What specific error message
    did you get? Is this your actual code, or did you make a transcription
    error when re-typing it? (if so, please copy/paste code instead of re-
    typing it).

    There is a bad logic error in the above line. You have manually pd
    out the CGI parameters into the %FORM variable. So $FORM{$key} is the
    value of the $key CGI variable. However, you mysteriously then pass that
    to the CGI.pm param() function, which does the same thing. So if your
    checkbox is named "c1", and its value is "yes", then you're calling
    param("yes"), which attempts to look up the value of the "yes" variable.
    Either use %FORM or use param(), not both. Since I highly recommend that
    you ditch your %FORM variable, read that as "use param($key) instead"!

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP3LPUGPeouIeTNHoEQKx2ACeMdMroTBTyTeB23pC+G2dVb ClUJkAoOkD
    fKQCDcDT3D8vD+eeBrELUqiH
    =JBwi
    -----END PGP SIGNATURE-----
    Eric Guest

Similar Threads

  1. Checkboxes keep selected data but show all choices inupdate form
    By tharring in forum Coldfusion - Getting Started
    Replies: 1
    Last Post: February 22nd, 05:12 PM
  2. Flash Form Checkboxes
    By johnnyred in forum Macromedia ColdFusion
    Replies: 4
    Last Post: February 17th, 05:19 PM
  3. newbie form authentication
    By SStory in forum ASP.NET Security
    Replies: 1
    Last Post: September 23rd, 09:49 PM
  4. adding checkboxes in a form
    By marc in forum Microsoft Access
    Replies: 0
    Last Post: July 31st, 12:18 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139