Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

passing an array to a checkbox_group

by amulcahy (Acolyte)
on Oct 11, 2001 at 13:46 UTC ( #118182=perlquestion: print w/ replies, xml ) Need Help??
amulcahy has asked for the wisdom of the Perl Monks concerning the following question:

New to Perl so be nice to me: My 'while' loop creates a comma-delimited string ($str) like: 'Date','Forename','Middle','School'
while (<>) { if ($str eq "") { $str = "'" . substr($line,0,$x) . "'"; } else { $str = $str . "," . "'" . substr($line,0,$x) . "'"; } }
I want to create a checkbox_group for this array to have a checkbox for each entry. The code I have creates one checkbox and the entire array is after it. i.e. checkbox 'Date','Forename','Middle','School'
@array = ($str); print $query->start_multipart_form("POST","printFields"); print $query->checkbox_group(-name=>'FieldNames', -value=>[ @array ]); print $query->submit('doWhat','Print Fields'); print $query->end_form;
If I change the code from
@array = ($str);
to
@array = ('Date','Forename','Middle','School');
it works the way I want it to. i.e. checkbox Date checkbox Forename checkbox Middle checkbox School. What am I doing wrong? Any ideas? AM

Comment on passing an array to a checkbox_group
Select or Download Code
Re: passing an array to a checkbox_group
by khippy (Scribe) on Oct 11, 2001 at 13:58 UTC

    First, you don't create an array when putting data to a
    $ variable, like you do to $str - It is a scalar, so
    @array = ($str); does not work.

    Instead, if possible use push to fill the array like this:
    while (<>) { push @array, $str ; }

    --

    there are no silly questions

    killerhippy
      That worked, thanks. I have a follow-up question:
      In the 'multipart_form' I set a param 'TblMenu' to $STable, but when I call it from another Perl script it doesn't recognise it.
      $STable = 'tab001'; print $query->start_multipart_form("POST","printFields"); print $query->checkbox_group(-name=>'FieldNames', -value=>[ @array ]); $query->param( -name=>'TblMenu', -value=>$STable); print $query->submit('doWhat','Print Fields'); print $query->end_form;
      Calling Code:
      $SFields = join(", ",$query->param('FieldNames')); $STab = $query->param('TblMenu');
      $SFields returns the selected checkboxes
      $STab returns nothing.
      I feel like there should be something like
      $STab = $query->param('FieldNames.TblMenu');
      but I don't know what it is?

      AM
        Is it a tip to include a print statement at line 5 ?
        --

        there are no silly questions

        killerhippy
        If I understand what you are asking, you seem to be wanting to set a variable in one script that outputs some html, and then access that variable's value the next time the form is submitted?

        If that's the case you need to be writing a hidden field into your first script's output that has the name and value you want to access in your second script. CGI.pm probably has a method for doing this.

        Guy

        my tests seem to work ok, but I am unclear on how you are calling this from another script. do you mean as the action or some other means?

        Update: seems to work when I added this after line 7 in the first script.

        print $query->hidden('TblMenu');

        is this what you were wanting?

        you have to pull the values from the checkbox with a param .. e.g.,
        @fieldnames=$query->param('FieldNames')
        and the resultant array is less tractable to splits and other parsing operations, so be prepared to use a substr() to separate out what you want.

      Alternatively (depending on memory constraints), you can simply do:

      @array = <>;

      which puts one line in each array index. If you need that substr call, consider this:

      @array = <>; @array = map { substr($_,0,$x) } @array; #or in one step: @array = map { substr($_,0,$x) } <>;

      HTH

Re: passing an array to a checkbox_group
by CubicSpline (Friar) on Oct 11, 2001 at 16:33 UTC
    TMTOWTDI

    while (<>) { if ($str eq "") { $str = "'" . substr($line,0,$x) . "'"; } else { $str = $str . "," . substr($line,0,$x); } } checkbox 'Date','Forename','Middle','School' @array = split ',',$str; print $query->start_multipart_form("POST","printFields"); print $query->checkbox_group(-name=>'FieldNames', -value=>[ @array ]); print $query->submit('doWhat','Print Fields'); print $query->end_form;
Re: passing an array to a checkbox_group
by guidomortonski (Sexton) on Oct 11, 2001 at 16:34 UTC
    The problem is that @array=($str) doesn't turn your string into an array.

    What you need to do is split $str:

    @array = split/,/, $str;

    This will give you the array of values you are looking for.

    Guy

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-09-16 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (44 votes), past polls