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

I basically learned Perl by altering existing scripts (yeah, not the best, I know). I need to generate multiple unique checkboxes in an HTML page, then see if any of them have been checked.

The whole little testing script is below. If I check for simple checkedness, ie, "if ($value)", nothing shows as checked. Likewise for "if ($value eq 'on')" (or any string, or number). If I try "if ($value == 'any damn string')" EVERTHING is on.

I suspect the problem is actually how I am getting the form parameters into the $form_data array, which is a piece of apparent magic from the original script I started with and seems to have worked fine for all the other field types in the actual real cgi program.

The script can be run from http://www.toukleyartgallery.com.au/cgi-bin/checktest.htm

#!/usr/bin/perl -w BEGIN { open (STDERR, ">test.err");} use CGI qw(:standard); use CGI::Carp qw/fatalsToBrowser/; $ENV{"PATH"} = ""; print "Content-type: text/html\n\n"; $query = new CGI; foreach $sparam ($query->param()) { $form_data{$sparam} = $query->param($sparam); } &check_test; sub check_test() { print qq~ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:/ +/www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> ~; print "<form name='form1' method='post' action='http://www.toukley +artgallery.com.au/cgi-bin/checktest.cgi'>\n"; for $i (1 .. 4) { for $j (1 .. 4) { if ($form_data{'box$i$j'} == "definitely not on") { print "<font color='#FF0000'>Box $i$j: $form_data{'bo +x$i$j'}</font> "; } else { print "Box $i$j: <input type='checkbox' name='box$i$j' +> " } } } print "\n<input type='submit' name='Submit' value='Submit'>\n</for +m>\n"; print "</body>\n</html>\n"; }

Replies are listed 'Best First'.
Re: Trouble testing if on-the-fly generated checkboxes are checked
by Your Mother (Archbishop) on Jun 06, 2018 at 04:46 UTC

    Without checking your code, just going on your post–

    perl -le 'print "YUM!" if "tamales" == "rellenos"' YUM!

    == forces numeric comparison which forces both string sides to zero, making them equal. Update: I should have said, use eq instead for string equality comparisons.

Re: Trouble testing if on-the-fly generated checkboxes are checked
by bliako (Abbot) on Jun 06, 2018 at 09:49 UTC

    From CGI man page :

    my @values = $q->multi_param('foo');
     
        -or-
     
    my $value = $q->param('foo');
     
        -or-
     
    
    my @values = $q->param('foo'); # list context, discouraged and will ra +ise # a warning (use ->multi_param instead)
    Pass the param() method a single argument to fetch the value of the named parameter. If the parameter is multivalued (e.g. from multiple selections in a scrolling list), you can ask to receive an array. Otherwise the method will return a single value.
    -----------------

    Maybe your $form_data{"box$i$j"} contained an array (edit: it is not correct, it will contain the length of the array) as returned by param($sparam}) in list content, whereas the param("box$i$j") eq "string" was forced to return a scalar, i.e. a single string. If that's the case you have some more debugging to do.

    In that case,

    use strict; use warnings;

    at the beginning of your program will be your friends.

Re: Trouble testing if on-the-fly generated checkboxes are checked
by goldgryph (Initiate) on Jun 06, 2018 at 02:55 UTC
    I've also tried explicitly passing the value "on" back in the <checkbox> tag. Same results.

      Single quotes do not interpolate

      # if ($form_data{'box$i$j'} == "definitely not on") if ($form_data{"box$i$j"} eq "on")
      poj
Re: Trouble testing if on-the-fly generated checkboxes are checked
by goldgryph (Initiate) on Jun 06, 2018 at 07:26 UTC
    Thanks guys, already tried all variations of eq and ==, single and double quotes (which was a problem, but not the main one). I discovered that using param("box$i$j") actually did the trick.