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

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);
@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

Replies are listed 'Best First'.
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

      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?

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

        there are no silly questions

        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. probably has a method for doing this.


        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.,
        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) } <>;


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

    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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://118182]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (16)
As of 2017-01-19 14:16 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (170 votes). Check out past polls.